Detailed Java class loading mechanism (recommended) _java programming

Source: Internet
Author: User

Java source Code compilation consists of three processes:

1, the source code compilation mechanism.

2. Class loading mechanism

3. Class execution mechanism

Here we mainly introduce the two mechanisms of compiling and class loading.

First, the source code compilation

Code compilation is done by the Java source compiler. The main is to compile the source code into bytecode files (class file). The bytecode file format is mainly divided into two parts: the constant pool and the method byte code.

Second, class loading

The lifecycle of a class starts from being loaded into the virtual machine memory to the end of unloading out of memory. There are seven stages in the process, which are part of the class load before initialization

Loading----Validation----Preparing----parsing-----Initialization----using-----Uninstall

The system may load the class the first time it is used. You might also use a preload mechanism to load a class, and when you run a Java program, you start a Java Virtual machine process, and two Java programs run in two different JVM processes, and no data is shared between two JVMs.

1. Loading stage

The load in this process is a phase in the class loading mechanism, and the two concepts don't confuse, and the things that need to be done at this stage are:

1 to obtain a binary byte stream that defines this class through a fully qualified name of a class.

2 converts the static storage structure represented by this byte stream into the RUN-TIME data structure of the method area.

3 generates a class object in the Java heap that represents it as a portal to access the data in the method area.

Since the 1th does not specify where to get and how to get the binary byte stream of the class, this area leaves me with a lot of room for developers to play. This I am introducing in the class loader that follows.

2. Preparation Stage

This phase formally allocates memory for the class variable (the variable that is modified by static) and sets the class variable initial value, which occurs in the method area.

1. Note that there is no memory allocation for instance variables, and instance variables will be assigned to the Java heap along with objects when the object is instantiated.

2, the initial value set here, usually refers to the data type of 0 value.

private static int a = 3;
The value of this class variable A in the preparation phase is 0, and assigning 3 to variable A is happening in the initialization phase.

3. Initialization phase

Initialization is the last step of the class loading mechanism, which is really starting to execute the Java program code defined in the class. In the previous preparation phase, the class variable has already assigned the initial value of the system requirement, and the most important thing in the initialization phase is to initialize the class variable, focusing on the sequence of various resource initialization between the parent and child classes.

There are two ways to specify an initial value for a class variable in a Java class: 1, specify an initial value when declaring a class variable, 2, and use a static initialization block to specify an initial value for a class variable.

Timing of initialization

1 When creating class instances, there are: 1, create an instance with the New keyword, 2, create an instance by reflection, and 3, create an instance by deserializing.

New Test ();
Class.forName ("Com.mengdd.Test");

2 The class method that invokes a class (static method)

Test.dosomething ();

3 access the class variable of a class or interface, or assign a value to the class variable.

int b=test.a;
Test.a=b;

4 Initializes a subclass of a class. When a subclass is initialized, all the parent classes of the subclass are initialized.

5) Use the Java.exe command directly to run a main class.

In addition to the above several ways to automatically initialize a class, other access classes are said to not trigger the initialization of the class, called a passive reference.

1. Subclasses refer to static variables of the parent class and do not cause subclasses to initialize.

public class Supclass
{public
 static int a = 123;
 Static
 {
  System.out.println ("Supclass init");
 }
public class Subclass extends Supclass
{
 static
 {
  System.out.println ("Subclass Init");
 }
Public
class Test
{public
 static void Main (string[] args)
 {
  System.out.println (SUBCLASS.A);
 }
}

Execution results:

Supclass Init
123

2, the reference class through the array definition does not trigger the initialization of this class

public class Supclass
{public
 static int a = 123;
 Static
 {
  System.out.println ("Supclass init");
 }
public class Test
{public
 static void Main (string[] args)
 {
  supclass[] spc = new SUPCLASS[10];
}

Execution results:

3, the initialization of the class is not triggered when the constant is referenced

public class Constclass
{public
 static final String a= "Migu";
 Static
 {
  System.out.println ("Constclass init");
 }
public class Testmain
{public
 static void Main (string[] args)
 {
  System.out.println (CONSTCLASS.A );
 }
}

Execution results:

Migu

When a class variable is decorated with final, its value is determined to be placed in a constant pool at compile time, so when accessing the class variable, it is equal to being fetched directly from the constant pool, and the class is not initialized.

Steps to initialize

1. If the class has not yet been loaded and connected, the program loads the class and joins it first.

2. If the direct parent class of the class is not loaded, the immediate parent class is initialized first.

3. If there are initialization statements in the class, the system executes the initialization statements sequentially.

In the second step, if the direct parent class has a direct parent class, the system repeats the three steps again to initialize the parent class, and so on, and so on, the JVM always initializes the Java.lang.Object class first. When a program actively uses any class, the system guarantees that the class and all of its parent classes will be initialized.

The above is a small series to introduce the Java class loading mechanism (recommended), I hope to help you!

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.