Creating objects in Java is one of the most common operations, and the new
keyword is at the center of it. The question is simple: what actually happens when you type new
in code? Behind that one word is a sequence of steps carried out by the Java Virtual Machine (JVM) to allocate memory, call constructors, and return an initialized object reference.
The short answer is that new
tells the JVM to grab space on the heap, attach a header so it can be managed, run the constructor chain to put all values in place, and then hand back a reference to the finished object. To see why those steps matter, it helps to look more closely at where objects are stored, how headers connect them to their class, how constructors set them up, and how memory is reclaimed when they’re no longer in use.
Where Objects Live
When you write new
, the JVM normally reserves space for the object in the heap (typically from a thread-local allocation buffer). If the JIT proves the object doesn’t escape, it can cut out the allocation through scalar replacement
. In HotSpot the object never goes on the Java stack, but the end result is the same as if a normal heap object had been created. The JVM uses free regions in the heap to place new objects, and if it runs out of space, it will trigger the garbage collector to make room before continuing.
Both p1
and p2
sit in separate memory slots on the heap, while the variables hold references to those slots.
Object Headers
Every object has a hidden header stored just before its fields. The header contains technical details that let the JVM work with the object. It has a mark word used for synchronization and garbage collection state, and it has a pointer to the class metadata that describes the object’s type. This way, when a method is called on an object, the JVM can follow that pointer back to the class and know what code to run.
Although you only see size
, the JVM actually stores the header first, then the integer field. You never interact with the header directly, but without it the JVM wouldn’t be able to handle features like synchronized
blocks or know which class layout to apply.
Running Initialization Code
After the memory is claimed, the JVM runs the constructor to set up the object. Fields are zeroed first. Next, the superclass constructor runs. After it returns, instance field initializers and instance initializer blocks of the current class run in textual order, then the rest of the constructor body executes.
The console prints “Parent ready” before “Child ready” because the JVM must finish the base part of the object before finalizing the derived part.
The This Reference
During construction, the object is already created in memory, so the keyword this
is a reference to it. This lets the constructor or other methods set fields on the object being created. Without this
, there would be no way to refer to the freshly allocated object during its own setup.
The reference this
is quietly passed into every non-static method, and during construction it makes sure the code is writing values into the right object.
Delivering the Result
After allocation and construction, the new
keyword produces a reference that points to the object in the heap. Assigning it to a variable means the variable doesn’t contain the object itself but rather a pointer to its memory location. If two variables are assigned the same object, they both carry references to the same memory.
Both b1
and b2
connect to the same memory slot, so comparing them with ==
confirms they share the reference.
Garbage Collection Connection
As long as at least one reference points to an object, the JVM keeps it alive. When no references remain, the garbage collector steps in and clears that memory for later reuse. The collector doesn’t run on a fixed schedule but rather when the JVM decides memory pressure calls for it. This automatic process keeps heap usage steady without requiring the developer to free objects manually.
The call to System.gc()
is only a request, not a command. If the collector runs, it sees that the string object no longer has references and reclaims its memory.