I\'m just learning about generics in Java from a textbook, where it talks about a class GenericStack
implemented with an ArrayList
Both the class and the constructor can be generic on their own, i.e. each has its own type parameters. For example
class A
{
A(S s){}
}
To invoke the constructor with all explicit type arguments
new A("abc"); // T=Integer, S=String
Think of it like this, A
is the concrete class, and
is the concrete constructor.
Because of type inference, we can omit the type argument for the constructor
new A("abc"); // S=String is inferred
The type argument for the class can also be omitted and inferred, by "diamond" (<>
) syntax
A a = new A<>("abc"); // T=Integer is inferred, as well as S=String
I wrote a (long) post on this subject - https://stackoverflow.com/a/32292707/2158288