When the Java program is running, the Java system has always carried out the so -called runtime type identification of all objects, the so -called RTTI. This information records the class of each object. The virtual machine usually uses the correct method of running the type information to be executed. The class used to preserve these types of information is the Class class. The Class class encapsulates the state of an object and interface. When the loading class is loaded, the Class type object is automatically created.
The simple summary is as follows:
The Class class is also a class, but the name is similar to that of the Class keyword. Java is a sensitive language.
The content of the class class is the type information of the class you created. For example, if you create a Shapes class, then Java will generate a class of the class class of Shapes
Objects of the class class cannot be created in a new shapes () manner like an ordinary class. Its object can only be created by JVM, because this class does not have the Public constructor function function function.
The role of the Class class is to provide or obtain a type information that provides or obtain a certain object during runtime, which is similar to the Typeid () function in C ++. This information can also be used for reflection.
1.class class principle
We all know that all the Java classes inherit the Object class. There is a method in the Object class: getClass (). This method is used to obtain the reference to the class that has been instantly instantiated. Quote pointed to the object of the Class class. We cannot generate a Class object (the constructor is Private), and the object of this class class is to automatically create the Class object by the Java virtual machine when it is transferred, or it is generated by the defineClass method in the loader. The objects we generate will have a field to record the location of the object of the object in the class class of the object. As shown in the figure below:
2. Get a Class class object
The first method is the forname function of the class class
[codec class shapes {}
Class obj = class.Forname ("shapes"); [/code]
The second method is to use the object of the object of the object
Copy code code as follows: class shapes {}
shapes s1 = new shapes ();
Class obj = s1.getClass ();
Class obj1 = s1.getsuperclass (); // This function is the type of the parent class of the Shapes class
The third method is to use the type of type constant
Copy code code as follows: obj = string.class;
Class obj1 = int.class;
Note that when using this method to generate the Class class object, JVM will not automatically load the class (such as String). Other methods will make JVM initialize this class.
3. Use the object of the Class class to generate an instance of the target class
Inaccurate Object instance
After obtaining an object of the class class, you can use the newInstance () function to generate an instance of the target class. However, this function does not directly generate instances of the target class, and can only generate instances of the Object class
Copy code code as follows: obj = class.Forname ("shapes");
Object shapesinstance = obj.newinstance ();
Use generalization Class to reference the target instance of the type belt type
Copy code code as follows: shapes> obj = shapes.class;
shapes newShape = Obj.newinstance ();
Because of the type limit, the object reference to the generalization Class syntax cannot be directed to other categories.
Copy code code as follows: class obj1 = int.class;
Class <integer> obj2 = int.class;
obj1 = double.class;
//Obj2=double.class; This line code is illegal, OBJ2 cannot be changed to other categories
However, there is a flexible usage that allows you to point to any subclass of the base class with objects of Class.
Copy code code as follows: class <? Extends number> obj = int.class;
obj = number.class;
obj = double.class;
Therefore, the CLASS object generated below can point to any class.
Copy code code as follows: class <?> Obj = int.class;
obj = double.class;
obj = shapes.class;
The last strange usage is that when you use this generic syntax to build a base class object of an object of a class class you have at hand, you must use the following special grammar
Copy code code as follows: Public Class Shapes {}
Class Round Extends Shapes {}
Class <rand> rClass = round.class;
Class <? Super Round> sclass = rclass.GetsuperClass ();
// class <shapes> sclass = rclass.getsuperclass ();
We know that the base class of Round is shapes, but it cannot directly declare the class <shapes> and must use a special grammar
Class <? Super Round>
The above is all the contents of this article. I hope it will be helpful to everyone's learning.