This article mainly introduces the role of the Class class in Java and related information for in-depth understanding. I hope this article can help everyone understand this part of the content. Friends in need can refer to it
The role and in-depth understanding of the Class class in Java
During the running of the program, the Java runtime system always maintains a type identifier called runtime for all objects. This information keeps track of the class to which each object belongs. The JVM uses runtime information to select the appropriate method for execution. The class that holds this information is called Class. It may be easy to cause confusion, and it is easy to think of class. However, the two have nothing to do with each other. Class is just a keyword describing a class. Class is a class that stores runtime information.
What can it do? The Class class can help us analyze the class when the program is running. To put it bluntly, it is to obtain the value in the class. Maybe I immediately thought of reflection, yes! Class is generally used in conjunction with reflection, because we provide a class or the class name of a class to Class, and Class can provide us with a lot of information, such as attributes/methods/modifiers/constructors/class names, etc. Then we can go further with reflection. However, let’s first briefly understand the content and usage of the Class class!
We usually use the following method to obtain the Class object:
String str = new String(); Class cl = str.getClass();
Because this class contains too much information, we can use it to obtain a specific class fields/methods and constructors. Here are some commonly used methods:
public static void main(String[] args) throws Exception { // 以String.class为例 String str = new String(); Class cl = str.getClass(); /** * 获取包名+类名<br> * java.lang.String */ cl.getName(); /** * 只获取类名 - String */ cl.getSimpleName(); /** * 获取数组的Class对象<br> * 因为所有的Java类都继承自Object,数组也一样.所以数组本身也是个Class, 得到数组的Class自然也可以转回数组. */ cl.getComponentType(); /** * 返回构造器数组,包括超类的公有成员. */ cl.getConstructors(); /** * 返回方法数组,包括超类的公有成员. */ cl.getMethods(); /** * 返回域数组,包括超类的公有成员. */ cl.getFields(); /** * 返回全部构造器数组,无论是public/private还是protected,不包括超类的成员. */ cl.getDeclaredConstructors(); /** * 返回全部域数组,无论是public/private还是protected,不包括超类的成员. */ cl.getDeclaredFields(); /** * 返回全部方法数组,无论是public/private还是protected,不包括超类的成员. */ cl.getDeclaredMethods(); /** * 获取类前的修饰符 */ cl.getModifiers(); }
We can obtain the Class object through a class and then obtain this type of information. You can also get the Class object by its full class name.
Class cl = Class.forName("java.lang.String");
With the Class object of this class, we can create objects of this class. The most convenient/fast way is to call newInstance(). By default, it calls the no-argument constructor to return an object.
String str = (String)(Class.forName("java.lang.String").newInstance());
The following is using Constructor to create an object by getting the constructor:
##
// 调用无参的私有构造函数 Constructor c1 = Class.forName("java.lang.String") .getDeclaredConstructor(); c1.setAccessible(true); String str1 = (String) c1.newInstance(); // 调用有参的私有构造函数 Constructor c2 = Class.forName("java.lang.String") .getDeclaredConstructor(new Class[] { String.class }); c2.setAccessible(true); String str2 = (String) c2.newInstance("hello");Class is not too difficult, because It is an information class. Track the class at runtime and master all the information about the class.
The above is the detailed content of Detailed explanation of the role of Class class in Java. For more information, please follow other related articles on the PHP Chinese website!