Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics Java and Shadowing of type parameters

Tags:

java

generics

This code seems to work fine

class Rule<T>
{

    public <T>Rule(T t)
    {

    }
    public <T> void Foo(T t)
    {

    }
 }
  1. Does the method type parameter shadow the class type parameter?
  2. Also when you create an object does it use the type parameter of the class?

example

Rule<String> r = new Rule<String>();

Does this normally apply to the type parameter of the class, in the situation where they do not conflict? I mean when only the class has a type parameter, not the constructor, or does this look for a type parameter in the constructor? If they do conflict how does this change?

SEE DISCUSSION BELOW

if I have a function call

x = <Type Parameter>method(); // this is a syntax error even inside the function or class ; I must place a this before it, why is this, and does everything still hold true. Why don't I need to prefix anything for the constructor call. Shouldn't Oracle fix this.
like image 284
rubixibuc Avatar asked Apr 14 '12 04:04

rubixibuc


People also ask

Can generics take multiple type parameters?

A Generic class can have muliple type parameters.

What is a generic type parameter in Java?

Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.

Can generic class handle any type of data?

A generic class and a generic method can handle any type of data.

What is generics in Java What are advantages of using generics?

In a nutshell, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs.


1 Answers

All of your Ts are different, but you can only see it if you call your methods with the complete syntax:

For example, this code is valid:

new <Float>Rule<Integer>().<Character>Foo();

Just to make this easier to explain, let's assume your code is this:

class Rule<A>
{

    public <B>Rule()
    {

    }
    public <C> void Foo()
    {

    }
 }

Then you can explicitly declare generic types like:

new <B>Rule<A>().<C>Foo();

If the types have the same name, the inner-most one will be chosen (the T on the method, not the class):

With this code, taking parameters:

class Rule<T>
{

    public <T>Rule(T t)
    {

    }
    public <T> void Foo(T t)
    {

    }
}

Then this is valid:

new <Float>Rule<Integer>(3.2f); 

Note that T in the constructor is Float, not Integer.

Another example:

class Example<T> {

    public <T> void foo1() {
        // T here is the <T> declared on foo1
    }

    public void foo2() {
        // T here is the <T> declared on the class Example
    }
}

I found another question that deals with calling methods with explicit generic types without something before them. It seems like static imports and same-class method calls are the same. It seems like Java doesn't let you start a line with <Type> for some reason.

like image 183
Brendan Long Avatar answered Sep 30 '22 18:09

Brendan Long