Class is a class in the java.lang package. Instances of this class can help programs create instances of other classes or obtain internal information about objects of other classes.
The most common way to create an object is to use the new operator and the constructor of the class. In fact, you can also use the Class object to get an instance of a certain class. The steps are as follows:
(1) Use the class method of Class to get a Class object related to a certain class (the class specified by the parameter className):
publicstaticClassforName(StringclassName)throwsClassNotFoundException
The above method returns a Class object related to the class specified by the parameter className. If the class is in a package, className must contain the package name, for example: className=java.util.Date.
(2) The Class object obtained in step (1) calls public Object newInstance() throws InstantiationException, IllegalAccessException method to get an object of className class.
Note : When using the Class object to call newInstance() to instantiate an object of the className class, the className class must have a parameterless constructor.
When a class is loaded and an object is created, an object of type Class related to the class will be automatically created. Any object calling the getClass() method can obtain a Class object related to the object. The Class object is called as follows The method can obtain relevant information about the object, such as: the name of the class that created the object, the name of the method in the class, the name of the member variable, etc.
The methods frequently called by Class objects are as follows:
Returns the name of the class.
Returns all constructors of the class.
Returns all member variables of the class.
Returns all methods of the class.