Java Rtti Note Class learning note (from Java programming Idea)

Source: Internet
Author: User

1. Java uses the class object to perform its rtti. Each class in Java generates a Class object after compilation (more appropriately, in a. class file of the same name), and even void and the base type correspond to a class object.

The JVM uses the class loader to dynamically load the required classes into the JVM. All are dynamically loaded into the JVM the first time they are used. This class is loaded when the program creates the first static member reference to a class. A constructor is also a static method of a class, even if there is no static keyword, so a reference to a static member of a class is also used when creating a new object using the new operator.

Class loads the class object it first loads, and then finds the appropriate. class file if it is not loaded. Of course, some validation needs to be done.

The static block of code is executed when the class is loaded. The preparation to use the class actually consists of three steps:

(1) load, which is performed by the ClassLoader of the class. This step looks for the bytecode and creates a class object from the bytecode.

(2) connection, in the connection phase, validates the bytecode in the class, allocates storage space for the static domain, and resolves all references to other classes created by this class.

(3) Initialize, if the class has a superclass, initialize it, execute static initializers, and static code blocks.

2. Class has many static methods, such as forname (), which are loaded if there are no corresponding classes at the time of invocation. Class.forName ("ClassName") also returns a reference to a class object named ClassName. Class Newinstance () is used to create an instance of the type.

3. Java provides another way to produce a reference to a class object that uses a class literal constant. such as Myclass.class. Using this method is not only safe but also more efficient. However, the initialization of the method class is deferred until the first reference to the static method or to the very few static fields is executed.

4, the generalization of the class reference, the direct class object can point to different types of class objects, such as

Class intclass = Int.class;
class<integer> genericintclass = Int.class;
Genericintclass=double.class;//error

The book recommends the use of class< Extends myclass>

Java Rtti Note Class learning note (from Java programming Idea)

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.