Loading, joining, initializing of classes in Java

Source: Internet
Author: User

Static variables for all instances of the same class are shared with a block of memory.

However, data is not shared between two JVMs. After the class is loaded, the system generates a corresponding class object for it.

1. Loading of classes

Reads the class file of the classes into memory and creates a class object.

2. Connect

The process of connecting is divided into three steps:

(1) Verification

The internal structure of the loaded class is correct and coordinated.

(2) Prepare

Allocates memory for class variables and sets default initial values.

(3) Analysis

Replaces a symbolic reference in the binary data of a class with a direct reference.

3. Initialization

The virtual machine is responsible for initializing the class, primarily initializing the class variables.

1. Specify an initial value when declaring a class variable.

2. Use static initialization blocks as class variables.

The JVM Initializes a class that consists of the following steps:

1. If the class has not been loaded and connected, load and connect the class first.

2 If the immediate parent class of the class has not been initialized, its immediate parent class is initialized first.

So when will the system initialize the class?

When the Java program first uses the class or interface in the following 6 ways, the system initializes the class and interface.

(1) Create an instance of the class, including the new operator, to create an instance, and to create the inverse sequence by reflection.

(2) Call a class method of a class (static method)

(3) A class variable that accesses a class or interface or assigns a value to that class variable.

Note: For a final type class variable, the value of the class variable can be determined at compile time. does not cause initialization of the class.

Loading, joining, initializing of classes in Java

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.