java.lang.Class
, which is declared to be public final class Class<T>
, the parameter type <T>
represents the actual type modeled by this Class
object. One benefit is that some of its methods can take or return specific types, avoiding casting while enforcing type-check.It's also easy to write your own parameterized class. The following is a
Member
class whose Id field can be parameterized to String, Integer, etc:package javahowto;
public class Member<T> {
private T id;
public Member(T id) {
this.id = id;
}
public T getId() {
return id;
}
public void setId(T id) {
this.id = id;
}
public static void main(String[] args) {
Member<String> mString = new Member<String>("id1");
mString.setId("id2");
System.out.printf("id after setting id: %s%n", mString.getId());
//output: id after setting id: id2
Member<Integer> mInteger = new Member<Integer>(1);
mInteger.setId(2);
System.out.printf("id after setting id: %d%n", mInteger.getId());
//output: id after setting id: 2
}