Quick revision
The short answer
new Order(id) compiles to a new instruction followed by an invokespecial call to the constructor. At runtime, the JVM first makes sure Order is loaded, linked and initialized, then allocates memory, usually by bumping a pointer in the thread's own allocation buffer, and sets every field to its default value. The constructor then runs superclass constructors first, then Order's field initializers and instance blocks, then its body, and only when it completes does the caller get the reference. If escape analysis shows the object never leaves the method, the JIT may skip the allocation entirely.
What to remember
In this answer, we followed new Order(id) from bytecode to a usable object. The new instruction triggers loading, linking and initialization of the class if needed, allocates memory with a fast pointer bump in the thread's TLAB and zeroes the fields. invokespecial then runs the constructor chain, superclass first, then field initializers and instance blocks, then the body. The caller gets the reference only if the constructor completes, and the JIT can remove the allocation altogether when escape analysis shows the object never escapes.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
new Order(id) compiles to a new instruction followed by an invokespecial call to the constructor. At runtime, the JVM first makes sure Order is loaded, linked and initialized, then allocates memory, usually by bumping a pointer in the thread's own allocation buffer, and sets every field to its default value. The constructor then runs superclass constructors first, then Order's field initializers and instance blocks, then its body, and only when it completes does the caller get the reference. If escape analysis shows the object never leaves the method, the JIT may skip the allocation entirely.
Why this question is asked
We write new thousands of times a day, and most of the time it costs almost nothing. Knowing what it actually does explains several things we meet in real work: why allocation is cheap, why a field is null inside a constructor, why a class's static initializer runs at a seemingly random moment, and why some allocations disappear from a profile. Interviewers ask this question to see how deep our model of the JVM goes. In this answer, we will look at the bytecode, then follow the runtime steps in order: class initialization, allocation, the constructor chain, returning the reference, and finally what the JIT compiler can do to remove the allocation.
1The Bytecode Behind One Line
javac turns the expression into a small, fixed pattern of instructions.
Notice that allocation and construction are two separate instructions. new creates an uninitialized object and pushes its reference. dup copies the reference, because invokespecial consumes one copy as this for the constructor, and we need the other copy afterwards to store in o. The JVM's verifier makes sure nothing can use the object between new and the end of the constructor call, so bytecode cannot touch an uninitialized object by accident.
2Load, Link and Initialize the Class
Before it can allocate an Order, the JVM must know its layout, so the new instruction first resolves the class. If Order has not been loaded yet, a class loader finds Order.class and the JVM parses it. Linking then verifies the bytecode, prepares static fields with default values, and resolves symbolic references as needed.
Finally, new is one of the actions that triggers class initialization. If Order is not initialized yet, the JVM runs its static initializers, with superclasses initialized first, under the class's initialization lock. This happens once per class. It is why the first new Order(...) in a program can be much slower than the rest, and why a failing static initializer shows up at an unexpected line that merely creates an object.
3Allocate the Memory
Now the JVM knows how many bytes an Order needs: an object header plus the fields of Order and all its superclasses, aligned to 8 bytes. The header holds the mark word, used for hashing, locking and GC, and a pointer to the class. On Java 25 with compact object headers enabled (-XX:+UseCompactObjectHeaders), the header shrinks from 12 to 8 bytes, which saves memory for small objects.
Most allocations take a fast path. Each thread owns a Thread-Local Allocation Buffer, or TLAB, a chunk of the young generation reserved for it. Allocating means checking that the object fits and moving the buffer's top pointer forward by the object's size, which is a handful of machine instructions with no locking. Only when the TLAB is full does the thread ask for a new one, and large objects may be allocated directly elsewhere.
The memory is then zeroed, so every field starts at its default value: null for references, 0 for numbers, false for booleans. This is the state in which all the constructors begin.
4Run the Constructor Chain
invokespecial calls Order's constructor, internally named init. Every constructor starts by calling another constructor, either this(...) in the same class or super(...), so the chain climbs to Object first. Once the superclass constructor returns, Order runs its instance field initializers and instance initializer blocks in source order, which javac has copied into the constructor, and then the rest of its body.
- Order initialized?load, link, run static initializers onceYesContinueNoLoad, link and initialize Order
- Allocate in the TLABbump a pointer; slow path if the buffer is full
- Zero the memory, write the headerevery field is null, 0 or false
- invokespecial Order.<init>(id)calls
super(...)up to Object - Field initializers and instance blocksin source order, after super returns
- Constructor body
this.id= id - Constructor returns normally?YesReference stored in oNoException: caller never gets o
7 steps in order. Play them, or step through them one at a time.
Since Java 25, flexible constructor bodies also allow statements before the super(...) call, for example to validate arguments, but they cannot use the object being built, apart from assigning its fields.
5Hand Back the Reference
The caller only stores the reference after invokespecial returns normally. If the constructor throws, the exception propagates, the assignment to o never happens, and the half-built object becomes unreachable and is collected later. So from the caller's point of view, an object is either fully constructed or does not exist.
There is one way to break that promise: if the constructor itself leaks this, by registering the object or storing it in a shared field, other code can see it before construction finishes. That is why constructors should not let this escape. Also, final fields get their special visibility guarantee for other threads at exactly this point, when the constructor completes.
6The JIT May Remove the Allocation
Everything above describes the interpreter's view. When the JIT compiler optimizes a hot method, it runs escape analysis: it checks whether the new object can ever be seen outside the method or the thread. If not, for example an Order created, used to compute a total and then dropped, the compiler can apply scalar replacement, keeping the fields in registers or on the stack and never allocating the object at all.
This only works when the constructor and the methods called on the object are inlined, so the compiler can see everything that happens to it. It is one more reason why short-lived objects are so cheap in modern Java.
7Follow-up questions
Where does the object go if it is too big for the TLAB?
The thread either retires its current TLAB and takes a new one, or, for objects that are large relative to the TLAB size, allocates directly in the shared young generation with an atomic operation. Very large objects, such as big arrays, may go to special regions; G1 calls these humongous objects. The details depend on the garbage collector.
Is the constructor the same thing as the init method?
Yes. In the class file, every constructor is compiled to a special method named init, and invokespecial is the instruction used to call it. Static initializers are compiled to a different special method, clinit, which the JVM calls itself during class initialization.
What if two threads create the first Order at the same moment?
Both threads reach class initialization. One runs Order's static initializers while the other waits on the class's initialization lock, and once it is done, both continue to allocate their own objects normally. Each allocates in its own TLAB, so the allocations do not contend.
8Conclusion
In this answer, we followed new Order(id) from bytecode to a usable object. The new instruction triggers loading, linking and initialization of the class if needed, allocates memory with a fast pointer bump in the thread's TLAB and zeroes the fields. invokespecial then runs the constructor chain, superclass first, then field initializers and instance blocks, then the body. The caller gets the reference only if the constructor completes, and the JIT can remove the allocation altogether when escape analysis shows the object never escapes.
Open with
In bytecode it is new, dup, the arguments, invokespecial on the constructor, and a store. At runtime, new first resolves Order: if it is not loaded, a class loader loads it, the JVM links it, which includes verification, and since new triggers initialization, it runs the static initializers once, superclasses first, under the init lock.
Then, if they want more
Then it allocates. The JVM knows the size from the class layout, header plus fields, and on the fast path it just bumps a pointer in the thread's TLAB, a thread-local chunk of the young generation, with no locking. The memory is zeroed, so every field starts as null, zero or false. invokespecial then runs the constructor.
Each constructor calls this or super first, so the chain goes up to Object; after the superclass constructor returns, Order's field initializers and instance blocks run in source order, and then the constructor body.
The reference is stored in the variable only when the constructor returns normally; if it throws, the caller never gets the object, unless the constructor leaked this. And in hot code the JIT's escape analysis can see that the object never escapes the method and use scalar replacement, so the allocation disappears completely.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What steps happen when new Order(id) executes, from the bytecode to a usable object?": https://scrivio-interview-library.netlify.app/java/classes-and-objects/8ab87d2a37/