There's a good discussion of Generics and what they really do behind the scenes over at this question, so we all know that Vector<int[]>
is a vector of integer arrays, and HashTable<String, Person>
is a table of whose keys are strings and values Person
s.
However, what stumps me is the usage of Class<>
.
The java class Class
is supposed to also take a template name, (or so I'm being told by the yellow underline in eclipse). I don't understand what I should put in there. The whole point of the Class
object is when you don't fully have the information about an object, for reflection and such. Why does it make me specify which class the Class
object will hold? I clearly don't know, or I wouldn't be using the Class
object, I would use the specific one.
Best Answer
All we know is "All instances of a any class shares the same java.lang.Class object of that type of class"
e.g)
Then
a.getClass() == b.getClass()
is true.Now assume
without generics the below is possible.
But this is wrong now ..?
e.g)
public void printStudentClassInfo(Class studentClassRef) {}
can be called withTeacher.class
This can be avoided using generics.
Now what is T ?? T is type parameters (also called type variables); delimited by angle brackets (<>), follows the class name.
T is just a symbol, like a variable name (can be any name) declared during writing of the class file. Later that T will be substituted with
valid Class name during initialization (
HashMap<String> map = new HashMap<String>();
)e.g)
class name<T1, T2, ..., Tn>
So
Class<T>
represents a class object of specific class type 'T
'.Assume that your class methods has to work with unknown type parameters like below
Here T can be used as
String
type as CarNameOR T can be used as
Integer
type as modelNumber,OR T can be used as
Object
type as valid car instance.Now here the above is the simple POJO which can be used differently at runtime.
Collections e.g) List, Set, Hashmap are best examples which will work with different objects as per the declaration of T, but once we declared T as String
e.g)
HashMap<String> map = new HashMap<String>();
Then it will only accept String Class instance objects.Generic Methods
Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.
The syntax for a generic method includes a type parameter, inside angle brackets, and appears before the method's return type. For generic methods, the type parameter section must appear before the method's return type.
Here
<K, V, Z, Y>
is the declaration of types used in the method arguments which should before the return type which isboolean
here.In the below; type declaration
<T>
is not required at method level, since it is already declared at class level.But below is wrong as class-level type parameters K, V, Z, and Y cannot be used in a static context (static method here).
OTHER VALID SCENARIOS ARE
And finally Static method always needs explicit
<T>
declaration; It wont derive from class levelClass<T>
. This is because of Class level T is bound with instance.Also read Restrictions on Generics
Wildcards and Subtyping
type argument for a generic method