Deep understanding of Java type information (class object) and reflection mechanism

Source: Internet
Author: User
Tags reflection

Deep understanding of Class objects
  
The concept of Rrti and the role of class objects
  
Before you know the class object, understand a concept, RTTI (run-time type identification) run-time type recognition, for which the term is always a concept in C + +, and the Rrti in Java comes from the thinking in Java book, which is to identify the type of an object at runtime and the information of the class, there are two kinds: the traditional "RRTI", it is assumed that we know all the types at compile time (when the class object is created and used without the reflection mechanism, it is generally the compile period has determined its type, The class must be defined as the new object, and the other is the reflection mechanism, which allows us to discover and consume type information at run time. The corresponding class used to represent runtime type information in Java is class, which is also a real class that exists in the JDK's Java.lang package with some of the following source code:
  
  
The object that is created by the class is the object, and notice that the object represents the type information of the class that you manually write, such as creating a shapes class, and the JVM creates an object of the shapes class that holds the type information related to the Shapes class. In fact, in Java each class has an object, whenever we write and compile a newly created class will produce a corresponding object and this object will be saved in the same name. class file (the compiled bytecode file is the object), then why do you need such an object? So, when we new or reference a static member variable, the class loader subsystem in the Java Virtual Machine (JVM) loads the corresponding object into the JVM, and the JVM then creates a reference value for the object that we want to use for that type of information, or to provide a static variable. It is important to note that each class class that is written manually, regardless of how many instance objects are created, has only one object in the JVM, that is, in memory, there is only one counterpart object in each of the classes, quite a mouthful, by understanding (the simple in memory diagram):
  
Here we can also draw the following information:
  
Classes are also classes that are not the same as the class keyword.
  
A manually written class is compiled to produce a class object that represents the type information of the class being created, and the class object is stored in a file of the same name. Class (bytecode file), such as creating a shapes class, When you compile the shapes class, you create a class object that contains information about the type of the shapes class, and it is saved in the Shapes.class bytecode file.
  
Each class identified by the keyword class, in memory, has only one class object corresponding to it describing its type information, regardless of how many instance objects are created, based on a class object.
  
Class only has a private constructor, so the corresponding object can only have the JVM create and load
  
The object function of a class is to provide or obtain type information for an object at run time, which is important for reflection technology (for reflection later on).
  
Object loading and how to get it
  
Loading of objects
  
As we mentioned earlier, the class object is loaded by the JVM, so what is the loading time? In fact, all classes are dynamically loaded into the JVM for the first time they are used, and when the program creates the first static member reference to the class, it loads the used class (which actually loads the bytecode file for that class), and note that Creating a new instance object of a class using the new operator is also treated as a reference to the static members of the class (constructors are also static methods of the Class), so it appears that the Java program is not fully loaded into memory before they begin to run, and that the parts are loaded on demand, so when using the class, The ClassLoader first checks to see if the class object has been loaded (the class's instance object was created with the type information in a class object), and if it is not already loaded, the default ClassLoader will first find the. class file based on the class name ( After the compiled class object is saved in a. class file of the same name, when the bytecode file for this class is loaded, they must be validated to ensure that it is not corrupted and contains no bad Java code (this is the Java Security detection), which is dynamically loaded into memory when there is no problem at all. , the equivalent class object is loaded into memory (after all, the. Class bytecode file holds the class object), and it can be used to create all instance objects of the class. The following is a simple example of the timing of the class object being loaded (examples are referenced from thinking www.yszx11.cn/in Java):
  
}
  
}
  
}
  
}
  
}
  
}
  
}
  
1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21st
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21st
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
In the above code, there is a static statement for each class candy, Gum, Cookie, which is executed when the class is first loaded, and the function of this statement is to tell us when the class is loaded and the result is executed:
  
1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
From the result, new a Candy object and cookie object, the constructor will be called, is a reference to the static method, the class object of the candy classes and the class object of the cookie will certainly be loaded, after all, the candy instance object is created according to its class object. It's more interesting
  
1
  
1
  
Where the Forname method is a static member method of class, remember that all class objects originate from this class, so the methods defined in the class classes will adapt to all class objects. With the Forname method, we can get a class object reference to the gum class. From the print result, calling the Forname method will cause the gum class to be loaded (provided that the gum class has never been loaded).
  
Method
  
From the above case, we know that the call to the Class.forName () method will return a class object of the corresponding classes, so if we want to get the runtime type information of a class and use it, we can call Class.forName () method to get a reference to a class object, the benefit is that you do not have to get the class object by holding an instance object reference to that type, such as the 2nd way to get a class object from an instance object, where GetClass () is inherited from the top class object. It returns a class object reference that represents the actual type of the object.
  
try{
  
Get the Www.jyz521.com class object for gum classes by class.forname
  
}
  
Get gum class object from instance Object
  
}
  
1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
Note When you call the Forname method, you need to catch an exception named ClassNotFoundException, because the Forname method does not exist for the class in which the compiler is unable to detect the strings it passes, but only when the program is running. A ClassNotFoundException exception is thrown if it does not exist.
  
Literal constants
  
There is another way in Java to generate a reference to a class object, which is the class literal constant, as follows:
  
Get a Class object in a literal constant way
  
1
  
2
  
1
  
2
  
This approach is simpler and more secure than the previous two methods. Because it will be checked by the compiler at the same time because the compiler is not required to call the Forname method is also more efficient, because the literal method of obtaining a reference to a class object does not automatically initialize the class. What's even more interesting is that the literal constants that get the class object reference can be applied not only to ordinary classes, but also to interfaces, arrays, and basic data types, which can be helpful in the application of reflection technology to pass parameters, which will be analyzed later, due to the basic data types and the corresponding basic packaging types. Its wrapper type has a standard field type, and this type is a reference, a class object that points to the base data type, whose equivalent conversions are as follows, generally preferring to use the. Class form so that it can be maintained in the form of a common class.

Deep understanding of Java type information (class object) and reflection mechanism

Related Article

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.