NOTE
7.14 Object Creation Process
Java object creation involves allocation, constructor invocation, and storing a reference. These notes trace a new expression through bytecode.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Code
public class Test
{
private Integer val = 10;
public static void main(String[] args)
{
Test test = new Test();
}
}
2. Bytecode

2.1. Explanation
It can be seen that Test test = new Test(); is divided into three steps:
- Create a Test instance.
- Assign a value to the
valfield of the Test instance. - Assign the reference of the Test instance to the
testvariable.
After the first step is completed, the object is in a partially initialized state.
The issue is with steps 2 and 3. These two steps do not have a strong dependency, so according to CPU pipeline technology, these two instructions may be reordered, meaning the execution order may become 1-3-2.
2.2. Solution
Add the volatile modifier to the val variable to prohibit instruction reordering.
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub