Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Java's Class<T> generic?

Tags:

java

generics

Why is Java's Class<T> generic?

like image 557
ripper234 Avatar asked Jul 07 '09 18:07

ripper234


People also ask

What does T generic mean?

< 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.

What makes a class generic?

A class is generic if it declares one or more type variables. These type variables are known as the type parameters of the class.

What does T stand for in Java generics?

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.

What is the difference between a generic class and a generic method?

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.


2 Answers

So that generic typed methods can be used -

Class<Foo> klass = Foo.class; Foo f = klass.newInstance(); Foo f = klass.cast(Object); 
like image 68
Steve B. Avatar answered Oct 07 '22 06:10

Steve B.


Here is a reasonably good summary of the advantages: http://download.oracle.com/javase/tutorial/extra/generics/literals.html

like image 40
Chris Arguin Avatar answered Oct 07 '22 08:10

Chris Arguin