This code seems to work fine
class Rule<T>
{
public <T>Rule(T t)
{
}
public <T> void Foo(T t)
{
}
}
- Does the method type parameter shadow the class type parameter?
- 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.
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:
Just to make this easier to explain, let’s assume your code is this:
Then you can explicitly declare generic types like:
If the types have the same name, the inner-most one will be chosen (the
Ton the method, not the class):With this code, taking parameters:
Then this is valid:
Note that
Tin the constructor isFloat, notInteger.Another 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.