I want to define instance variables using two Generics like below.
class Foo<S,T>{
private S<T> Boo ;
.
.
}
public class Test{
public static void main(String[] args){
Foo<ArrayList, String> foo = new Foo<ArrayList, String>();
}
}
But It doesn't work... Is it kinda wrong grammar? I really need to this kind of grammar.
This is invalid syntax
S<T>
because for any x<T>, x must be a known class or interface type.
What you want is a container type and an element type. This is possible
class Foo<C extends Collection<E>, E>{
private C boo;
...
Foo<ArrayList<String>, String> foo = new Foo<>();
Maybe you need:
class Foo<S>{
private S Boo ;
.
.
}
public class Test{
public static void main(String[] args){
Foo<ArrayList<String>> foo = new Foo<ArrayList<String>>();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With