As shown below:
<span style="font-size:14px;">package com.imooc.reflect;public class ClassDemo1 {public static void main(String[] args) { //How does Foo represent Foo foo1 = new Foo();//foo1 represents //Foo this class is also an instance object, the instance object of Class class, how to represent it? //Any class is an instance object of Class, there are three representations of this instance object//The first representation--《actually tells us that any class has an implicit static member variable class Class class1 = Foo.class; //The second representation method already knows that the object of this class uses the getClass method Class class2 = foo1.getClass(); /* * Official website class1 , class2 represents the class type of Foo class (class type) * Everything is an object * Class is also an object, it is an instance object of Class class * We call this object the class type of this class*/ // No matter whether class1 or class2 represents the class type of Foo class, a class can only be Class; an instance object of the class System.out.println(class1==class2);//true' //The third expression method Class3 = null; try { class3 = Class.forName("com.imooc.reflect.Foo"); } catch (ClassNotFoundException e) { e.printStackTrace(); } // System.out.println(class2==class3);//true //We create object instances of this class entirely through the class type of the class--》 Through class1 or class2 or class3 //Create an instance object of Foo class try { //Constructor method with parameters is required Foo foo = (Foo) class1.newInstance();//Fo need to force foo.print(); } catch (Exception e) { e.printStackTrace(); }}}//class Foo{ public void print(){ System.out.println("foo"); }}</span>The above is the summary of the three representation methods of Java reflection and other example objects brought to you. I hope it will be helpful to you and support Wulin.com more~