Why is Java's Class<T>
generic?
< T > is a conventional letter that stands for "Type", and it refers to the concept of Generics in Java. You can use any letter, but you'll see that 'T' is widely preferred. WHAT DOES GENERIC MEAN? Generic is a way to parameterize a class, method, or interface.
A class is generic if it declares one or more type variables. These type variables are known as the type parameters of the class.
Generic Class Here, T is the data type parameter. T , N , and E are some of the letters used for data type parameters according to Java conventions. In the above example, you can pass it a specific data type when creating a GenericClass object.
A generic class or structure can contain nongeneric procedures, and a nongeneric class, structure, or module can contain generic procedures. A generic procedure can use its type parameters in its normal parameter list, in its return type if it has one, and in its procedure code.
So that generic typed methods can be used -
Class<Foo> klass = Foo.class; Foo f = klass.newInstance(); Foo f = klass.cast(Object);
Here is a reasonably good summary of the advantages: http://download.oracle.com/javase/tutorial/extra/generics/literals.html
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