Possible Duplicate:
Is it possible to reference a nested generic parameter in java?
A quick question on Java generics if I may. Is there syntax for declaring a generic class-wide type that is NOT used as a generic parameter for instantiation. For example:
public class <U extends FooUType> BarType<T extends FooType<U>>{
public U getU(){return U;}
}
To create a BarType I want to write the following, which in itself contains U, but I don’t want to have to specify U separately. So:
BarType<SomeT<FooUType>> instance
We get the type of U implicitly from the parameterized SomeT without having to specify U separately. As opposed to :
public class BarType<U extends FooUType, T extends FooType<U>>
which would require:
BarType<FooUType,SomeT<FooUType>>
I guess I’m looking for something akin to the same idea in methods:
public <U> boolean StupidMethod(String s){
...
}
I would rather not use <? extends FooUType> as this leads to problems with method return types inside the class that return <U extends FooUType> U.
Many thanks for the clarification!
Thanks all,
I went for a different solution in the end. Basically the class I was trying to hide the
Uin was abstract and so I addedUas a generic parameter as there is no way round this. I then created the concrete classes that extend this and had them fill in the type ofUsilently so that the caller wouldn’t need to bloat their code.Something like this:
where
SomeBaris not parameterized. The user can then just instantiate withT:This worked for my scenario neatly, so I hope it helps others.