1.
PackageReflectionz; Public classtreflectionz{ Public Static voidMain (string[] args)throwsClassNotFoundException {//get Cat's Class object (3 ways)//1, through the fornameclass<?> clazz1 = Class.forName ("Reflectionz.cat");//ZC: will report abnormal "classnotfoundexception"System.out.println ("CLAZZ1:" +clazz1+ "," +Clazz1.hashcode ()); //2. Through the class attributeClass<?> clazz2 = Cat.class; System.out.println ("CLAZZ2:" +clazz2+ "," +Clazz2.hashcode ()); //3, through the object instance. GetClass ()Cat cat =NewCat (); Class<?> clazz3 =Cat.getclass (); System.out.println ("Clazz3:" +clazz3+ "," +Clazz3.hashcode ()); System.out.println ("*** *** ***"); if(CLAZZ1 = =clazz2) System.out.println ("CLAZZ1 = = clazz2"); ElseSystem.out.println ("Clazz1! = clazz2"); if(CLAZZ1 = =clazz3) System.out.println ("CLAZZ1 = = clazz3"); ElseSystem.out.println ("Clazz1! = clazz3"); if(CLAZZ2 = =clazz3) System.out.println ("CLAZZ2 = = clazz3"); ElseSystem.out.println ("Clazz2! = clazz3"); //ZC: The above three judgments are equal, see the comparison of objects in Java, compared to the object that the variable points to, not the variable itself ... !!! }}classcat{}
2.
Console output:
Clazz1:class Reflectionz.cat, 8152936clazz2:class reflectionz.cat, 8152936clazz3:class reflectionZ.Cat, 8152936** * * * * ***CLAZZ1 = = Clazz2clazz1 = = CLAZZ3CLAZZ2 = clazz3
3.
Zc_01_ Get Class object