Java language class usage and generalization (in detail)

  • 2020-04-01 04:05:45
  • OfStack

This article mainly introduces the usage and generalization of class classes in the Java language (in detail). As you all know, Java programs perform type identification on all objects during the process of running, that is, RTTI. This information records the class to which each object belongs. Virtual machines typically use runtime type information to select the correct method to execute, and the Class that holds this type information is the Class Class. Class Class encapsulates the state of an object and interface at runtime. When the Class is loaded, the object of Class type is automatically created. The details are as follows:

Put simply:

A Class Class is also a Class, but the name is highly similar to the Class keyword. Java is a case-sensitive language.
The object content of the Class Class is the type information of the Class you create, so if you create the shapes Class, then Java generates an object with the Class content of shapes
The objects of the Class Class cannot be created in new shapes() like normal classes, and its objects can only be created by the JVM because the Class has no public constructor
The Class Class is used to provide or obtain type information for an object at runtime, similar to the typeid() function in C++. This information can also be used for reflection.

1. The principle of Class Class

We all know that all Java classes inherit from the Class object. In the Class object, there is a method: getclass(). This method is used to get the Class reference of the Class object whose Class has been instantiated. We can't generate a Class object ourselves (the constructor is private) that is automatically created by the Java virtual machine when the Class is called in, or by the defineClass method in the classloader. All the objects we generate have a field that records where the CLass belongs to the CLass's object. As shown in the following figure:

2. Get a Class object

The first is the forName function of the Class Class


public class shapes{} 
Class obj= Class.forName("shapes");

The second way is to use the object's getClass() function


public class shapes{}
shapes s1=new shapes();
Class obj=s1.getClass();
Class obj1=s1.getSuperclass();//This function gets the type of the parent class of the shapes class

The third way is to use literal constants of the class


Class obj=String.class;
Class obj1=int.class;

Note that when Class Class objects are generated in this way, the JVM does not automatically load the Class (such as the String Class). Other approaches cause the JVM to initialize the class.

3. Use the object of the Class Class to generate an instance of the target Class

Generates an inaccurate object instance

After obtaining the object of a Class Class, you can use the newInstance() function to generate an instance of the target Class. However, this function does not directly generate an instance of the target class, only an instance of the object class


Class obj=Class.forName("shapes");
Object ShapesInstance=obj.newInstance();

Generate a target instance with a type using a generic Class reference


Class<shapes> obj=shapes.class;
shapes newShape=obj.newInstance();

Because of the type constraint, an object reference that USES the generalized Class syntax cannot point to another Class.


Class obj1=int.class;
Class<Integer> obj2=int.class;
obj1=double.class;
//obj2=double.class;

This line of code is illegal, and obj2 cannot point to another class
However, there is a flexible usage that allows you to point to any subclass of the base Class with the object of the Class.


Class<? extends Number> obj=int.class;
obj=Number.class;
obj=double.class;

Therefore, the Class object generated by the following syntax can point to any Class.


Class<?> obj=int.class;
obj=double.class;
obj=shapes.class;

One final oddity is that when you use this generic syntax to build a base Class object for an object of a Class Class that you have on hand, you must use the following special syntax


public class shapes{}
class round extends shapes{}
Class<round> rclass=round.class;
Class<? super round> sclass= rclass.getSuperClass();
//Class<shapes> sclass=rclass.getSuperClass();


Note that the base Class for round is shapes, but it is not directly Class < Shapes > , must use the special syntax Class < The & # 63; Super round >

The above content introduction is for the Java language class class usage and generalization (in detail) all content, hope you enjoy the above content introduction.


Related articles: