I'm creating a Class object for class Box which has a type parameter. And in the place where I get Class objects it throws an warning that class type is raw - (Class objCB = b.getClass()).
My doubt is while creating a Class object for any class with type parameter why should generics come into the picture. And how to resolve the warning. Below is my code
//Box Class
public class Box <T extends Comparable<T>>{
public T objA;
public T objB;
public void set(T t1, T t2)
{
this.objA = t1;
this.objB = t2;
}
public int compare()
{
return this.objA.compareTo(this.objB);
}
}
// Refelection class
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class Test6 {
public static void main(String[] args) throws ClassNotFoundException {
//using getClass
Box <Integer> b = new Box <Integer>();
Class objCB = b.getClass(); //warning thrown here
Method[] methods = objCB.getMethods();
for (Method m : methods)
{
System.out.println(m.getName());
}
System.out.println("==================");
//using class
Class objC2 = Box.class; //warning thrown here
Field[] fields = objC2.getFields();
for (Field f : fields)
{
System.out.println(f.getName());
}
System.out.println("==================");
//using forName()
Class objC3 = Class.forName("Box"); //warning thrown here
Type[] types = objC3.getTypeParameters();
for (Type t : types)
{
System.out.println(t.toString());
}
System.out.println("==================");
//using Type()
Class objI = Integer.TYPE; //warning thrown here
System.out.println(objI.getName());
}
}