NOTE

7.4 Class Lifecycle

The class lifecycle: loading, linking, initialization, use, unloading, class loaders, parent delegation, and related questions.

JavaCreated Updated 2 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Class Lifecycle

The main steps are loading, linking, initialization, use, and unloading.

1.1. Loading

When our code needs to use a class and the class is not yet in the JVM, it will be loaded.

1.1.1. Who Loads It?

The class loader is responsible for loading classes.

1.1.1.1. Class-Loader Categories

  • The bootstrap class loader is responsible for loading JAVA_HOME/lib.
  • The extension class loader is responsible for loading JAVA_HOME/lib/ext.
  • The application class loader is responsible for loading classes specified by the user’s class path (ClassPath).
1.1.1.2. Parent Delegation Model

Class loading first requests the parent class loader to load the class. Only when the parent class loader cannot do so will the child class loader load it itself.

1.1.1.2.1. Why Is There a Parent Delegation Model?

Security.

For example, the String class is immutable. If we define a mutable String class ourselves and place it in the classpath directory, which class should be loaded when the program runs?

1.1.2. Loading Process

  1. Obtain the binary byte stream defining the class through the class’s fully qualified name.
  2. Convert the static storage structure represented by this byte stream into the runtime data structure of the method area.
  3. Generate a java.lang.Class object representing this class in memory, which serves as the access entry point for the class’s data in the method area.

1.2. Linking

1.2.1. Verification

According to the JVM specification, verify whether the contents of the loaded .class file comply with the specified rules.

1.2.2. Preparation

Allocate memory for class variables and set their default initial values (zero values of the data types / final values).

Class variables are variables declared with static, and they are allocated in the method area.

Note that instance variables are not allocated at this stage.

  • Example 1
public static int val = 100;

Here, val is initialized to 0 rather than 100.

  • Example 2
public final static int val = 300;

Here, val is initialized to 300 rather than 0.

1.2.3. Resolution

The process in which the virtual machine replaces symbolic references in the constant pool with direct references.

1.3. Initialization

Execute the class initialization code.

Class initialization code includes assignments to static fields and static code blocks.

The initialization rules are as follows:

Class Initialization Order

  • Example 1
public static int val = 100;

Here, val is initialized to 100.

  • Example 2
public static int val2 = 100;
static
{
    val2 = 200;
}

Here, val2 is initialized to 200.

1.4. Use

After new, the object instance is allocated in heap memory and referenced by a local variable in the stack frame.

Object Layout in Memory

1.5. Unloading

If an instance has no references pointing to it, it can be reclaimed by GC.

Garbage Collection

2. Questions

2.1. How Can a .class File Be Protected from Being Decompilied to Obtain a Company’s Source Code?

  • During compilation, small tools can be used to encrypt bytecode or perform obfuscation.
  • During class loading, encrypted classes can be decrypted using a custom class loader.

2.2. How Should a Class Loader in a Web Container Such as Tomcat Be Designed?

It breaks the parent-delegation mechanism. Each Web application has a corresponding WebApp class loader, which is responsible for loading the Web application’s classes rather than having the parent class loader load them.

3. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub