What Really Happens When Java Loads a Class and Creates an Object?
When Java executes a new expression, the JVM first ensures the class is loaded using the parent‑delegation model, then follows a five‑step loading and linking process before allocating heap memory, initializing fields, running static and instance code, and finally returning a reference to the new object.
Class Loading Process (First Use)
Loading
The class loader reads the binary byte stream of the class identified by its fully‑qualified name, stores it in the method area, and creates a corresponding java.lang.Class object.
Verification
Three kinds of verification are performed:
Format verification – checks that the .class file conforms to the class‑file specification.
Semantic verification – ensures, for example, that a final class has no subclasses and that final methods are not overridden.
Operation verification – validates operand‑stack operations and resolves symbolic references in the constant pool.
These checks guarantee compatibility between super‑ and subclass definitions.
Preparation
Memory is allocated for all static variables and they are given default values; final static constants receive their explicit values immediately.
Resolution (Linking)
Symbolic references in the constant pool are turned into direct references (pointers) to classes, fields, or methods. This step performs static binding for methods and fields that cannot be overridden. // 所有不会被重写的方法和域都会被静态绑定。
Initialization (Parent‑first)
Static variable assignments are executed.
Static initializer blocks run (invoked only by the JVM).
Only one thread may perform initialization; other threads wait until it finishes. The parent class is initialized before the child, and during parent initialization the child's static variables hold their default values. After this phase, the method area contains the class’s static fields, initialization code, instance field definitions, constructors, instance methods, and a reference to the superclass.
Object Creation Process
Heap Allocation
Memory is allocated in the heap for the new object, covering all instance variables declared in the class and its super‑classes (static variables are not included).
Default Field Initialization
Definitions of instance variables are copied from the method area to the heap and each field receives its default value.
Instance Initialization
Instance initializer blocks execute first, followed by the constructor. Initialization proceeds from the superclass down to the subclass.
Reference Assignment
A reference variable of the appropriate type is created on the stack and the heap address of the newly allocated object is stored in it.
Additional Notes
Each subclass object implicitly contains a reference to its superclass, accessible via the super keyword inside the class but not from outside.
Method invocation starts with a lookup in the actual class’s method area; if not found, the search proceeds up the inheritance chain, which can be costly for deep hierarchies.
To improve dispatch performance, the JVM builds a virtual method table (v‑table) for each class during loading. The v‑table holds addresses of all dynamically bound methods; overridden methods replace the superclass entry.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
