How the JVM Implements Java’s Object‑Oriented Model
The article walks through the JVM’s internal steps that turn a simple `new User()` statement into a fully formed Java object, detailing memory allocation, the object header’s type pointer, the Klass structure, and why methods are stored at the class level rather than in each instance.
To understand how Java’s object‑oriented features are realized inside the JVM, start from the familiar statement: User user = new User(); Although developers write such code daily, few consider what the JVM actually does to create the object.
The new keyword is merely syntactic sugar; the JVM performs several concrete steps:
The JVM locates the class User and, using its type information, calculates the exact amount of memory required for an instance, then allocates that space.
It writes a type pointer into the object’s header and initializes the field area as needed.
Only after these actions is the Java object considered fully created.
Even though the object now exists, a new question arises: how does it know which type it belongs to?
The heap stores only raw data fields such as name and age. The object header’s type pointer references the class’s type information, which tells the JVM the object’s class but not its methods.
Method information resides in a dedicated JVM data structure called Klass. This structure holds the class’s complete metadata, including method tables, field layout, and superclass links. When a call like user.save(); is made, the JVM follows the type pointer from the object header to the corresponding Klass, then looks up the save() method in that table. user.save(); The same lookup mechanism is used for getClass(), ultimately yielding a Java Class object.
Methods are not stored inside each object because a single class may have many instances; duplicating method code for every instance would waste memory. By keeping methods in the shared Klass and only a lightweight type pointer in each object, the JVM achieves both memory efficiency and fast method dispatch.
The article deliberately omits discussion of inheritance and polymorphism, focusing solely on the low‑level mechanism by which the JVM creates objects and resolves method calls.
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.
samdeepthink
Knowledge Planet: Old Dock's Tech Chronicles Zhihu: SamDeepThinking A technical manager who still codes heavily on the front line. From junior developer to tech lead, then tech manager, now leading the whole front‑ and back‑end development team—leveling up along the way. I have some insights on programming, career development, and tech management.
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.
