Java reflection mechanism analysis (III)-Class

Source: Internet
Author: User

Java reflection mechanism analysis (III)-Class
In order to distinguish it from the common Class, a Chinese name Class type is given. The purpose is to know that this Class is not another Class. The previous blog has introduced Class Loader. Its function is to load classes to the memory based on the provided information. I have mentioned this Class information before. The provider is the Class to be introduced in this blog. Reminder: The Class Object of a Class is different from its Instance. Remember, otherwise you will be confused. Started!
A conceptual Class is the abstraction of the common information of all classes (objects), such as the interfaces implemented by this Class, the corresponding loaders, and Class names. In a word, the class type stores the type information corresponding to each class. Each Class has a Class object, which is automatically constructed by JVM after the Class is loaded. It is also managed by JVM, and the Class does not have a public constructor.
A Class Object is like a Class. DNA contains all your biological information for everyone. In java, you can use the Class to obtain an instance of the Class. In the future, you may obtain another instance through your DNA. Science fiction movies have already been implemented. OK, the concept should have a preliminary understanding.
Common Methods

Method Introduction should not be so simple, but it is not clear to say a sentence or two, and it is helpful to Understand Java. Therefore, I decided to write a blog separately. Here are a few simple columns, which have previously been used.

ForName: returns the Class object associated with the Class or interface with the given string name. GetName (): A Class Object describes the attributes of a specific Class, the most common method in the Class, getName, returns the Object Name (Class, interface, array Class, basic type, or void) represented by this Class object in the form of String. NewInstance (): Creates a new instance of the Class Object Description type. The newInstance () method calls the default constructor (No parameter constructor) to initialize the new object. GetClassLoader (): Class Loader of the class returned. GetInterfaces (): determines the class or interface implemented by this object. GetComponentType (): returns the Class that represents the Array Component type. GetSuperclass (): returns the isArray () of the Class object that represents the super Class of the object (Class, interface, basic type, or void) represented by this Class (): determines whether the Class object represents an array Class.

There are three methods to obtain the Class Object:
(1) Use the Object. getClass () method to obtain the Class instance of the Object;
(2) Use the Class. forName () static method to obtain a Class instance with the Class name
(3) Use the. class Method of the Class to obtain the Class instance. For the encapsulation class of the basic data TYPE, you can also use. TYPE to obtain the corresponding Class instance of the basic data TYPE.
Note that the VM generates only one bytecode, which can generate multiple instance objects. That is to say, only one Class object exists. See the following code:
Test class
Public class Test {static {System. out. println ("static initialization") ;}{ System. out. println ("non-static initialization ");}}
Client
Public class client {public static void main (String [] arg) throws ClassNotFoundException, InstantiationException, IllegalAccessException {// method 1 Class c = Class. forName ("com. zjj. classTest. test "); // method 2 c = Test. class; // method 3 Test t = new Test (); c = t. getClass (); Test t2 = new Test ();}}
Output result:
Static Initialization
Non-static Initialization
Non-static Initialization
We all know that the static initialization method is executed when the class is loaded, and the non-static initialization method is executed when the class is instantiated. The output results only print "static initialization" once, which means that the three Class objects are the same.

That is to say, during the runtime, if we want to generate a Class object or get a Class object of a Class, the Java Virtual Machine (JVM) will check whether the Class object of this type has been loaded. If the file is not loaded, JVM will find the. class file based on the class name and load it. Once a Class object of a certain type has been loaded into the memory, it can be used to generate all objects of this type.

Conclusion Summary: At this point, we should be able to understand that Class is also a Class, but it is an abstraction of all classes, and the name is as confusing as the Class we know. In general, each Class has a corresponding Class object to store the Class information. This Class object is constructed and managed by JVM. The existence of Class objects is the basis of Java reflection.

Reflection mechanism Summary: The reflection mechanism is an important part of Java. It provides Java with the ability to load classes at runtime, that is, dynamic. Class is the information provider, Class Loader is the loading tool, and both are the most basic part of the reflection mechanism. The so-called reflection is to uncouple the Class by obtaining information about the unknown Class and then instantiating it. Of course, the work done by Class Loader is hidden and called by Class objects. Therefore, you do not need to display your own calls. So, the three blogs are now over!

By the way, you have to add a blog post from this blog. The content is not closely related to the reflection mechanism, but it helps to deepen your understanding of the Java operating mechanism.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.