The Class class is an implementation that defines a specific class in the Java language. A class definition includes member variables, member methods, interfaces implemented by this class, and parent class of this class. The object of the Class class is used to represent classes and interfaces in the currently running Java application. For example: Each array belongs to a Class class object, and all arrays with the same element type and dimension share a Class object. Basic Java types (boolean, byte, char, short, int, long, float and double) and void types can also be represented as Class objects.
The following example uses a Class object to display the Class name of an object:
void printClassName(Object obj) {System.out.println("The class of " + obj + " is " + obj.getClass().getName()); }We all know that all 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 of the object that has been instantiated. This reference points to the object of the Class class (hehe, it's a bit awkward). We cannot generate a Class object (constructor private), and the object of this Class class is automatically created by the Java virtual machine when various classes are called in, or generated by the defineClass method in the class loader. The object we generate will have a field to record the location of the object of the CLass class. As shown in the figure below:
We can treat each Class object as a proxy for many classes. Moreover, in each Class object, there will be a class loader that records the class it references. If this field is null, it means that the loader of the class is a bootstrap loader. For the specific reason, see the article "ClassLoader Working Mechanism" I shared before.
We know that there are multiple loaders in Java, and each loader can load multiple classes, so as long as you get the Class class object, you can use its getClassLoader() method to get the reference to the class loader.
jvm is a Class object unique to each type of manager. Therefore, we can use the biequal operator to compare objects: a1.getClass()==A.class; the one that should be returned is true.
forName(String classname) and forName(Stringclassname, {*}boolean initialze, *ClassLoader *loader)* methods.
This method returns the corresponding Class object with the given string name. Given the full pathname of a class or interface, this method attempts to locate, load, and connect the class. If successful, return the object of this class. Otherwise, throw a ClassNotFoundException exception. For example, the following code snippet returns a running Class descriptor named java.lang.Thread. Classst=Class.forName("java.lang.Thread"); This method requires specifying the class loader. When using the forName method with only one String parameter, the Class object will call the current class loader by default as the loader and set the second parameter to true. The second parameter description: If false, calling the forName method only loads the class in the command class loader, and does not initialize the static block of the class. The static block is called only when the class is instantiated for the first time. When true, the static block is called when loading.
getClassLoader()
Get the class loader for this class.
getComponentType()
If the current class represents an array, it returns the Class object representing the component of the array, otherwise it returns null.
getConstructor(Class[])
Returns the specified public constructor child object of the class represented by the current Class object.
getConstructors()
Returns an array of all public constructor subobjects of the class represented by the current Class object.
getDeclaredConstructor(Class[])
Returns a constructor subobject specified by the class represented by the current Class object.
getDeclaredConstructors()
Returns an array of all stated constructor subobjects of the class represented by the current Class object.
getDeclaredField(String)
Returns a domain object that has been specified for the class or interface represented by the current Class object.
getDeclaredFields()
Returns an array of all stated domain objects for the class or interface represented by the current Class object.
getDeclaredMethod(String,Class[])
Returns a method object that has been specified for the specified class or interface represented by the current Class object.
getDeclaredMethods()
Returns an array of all stated methods for the class or interface represented by the Class object.
getField(String)
Returns the specified public member domain object of the class or interface represented by the current Class object.
getFields()
Returns an array of accessible public domain objects for the class or interface represented by the current Class object.
getInterfaces()
Returns the interface implemented by the class represented by the current object or the interface.
getMethod(String,Class[])
Returns the specified public member method object of the class or interface represented by the current Class object.
getMethods()
Returns an array of all public member method objects for the class or interface represented by the current Class object, including declared and inherited from the parent class.
getModifiers()
Returns the Java language modifier code for the class or interface.
getName()
Returns the full path name string of the type (class, interface, array, or base type) represented by the Class object.
getResource(String)
Find resources by specified name.
getResourceAsStream(String)
Find resources with the given name.
getSigners()
Get the class tag.
getSuperclass()
If this object represents any class except Object, then the parent class object of this object is returned.
isArray()
Return true if the Class object represents an array, otherwise return false.
isAssignableFrom(Class)
Determines whether the class or interface represented by the Class object is the same as the class or interface represented by the Class specified by the parameter, or is its parent class.
isInstance(Object)
This method is a dynamic equivalent method for instanceof operations in Java language.
isInterface()
Determines whether the specified Class object represents an interface type.
isPrimitive()
Determines whether the specified Class object represents a Java base type.
newInstance()
Create a new instance of the class.
toString()
Converts an object to a string.
Summarize
The above is the entire content of this article about briefly discussing the class class in Java, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!