When initialising any class in java, does it create two objects?

java

Solution

Does it means JVM will create object for custom class and Object class?

No, it will only create an object for the custom class, but this object contains the `Object` class members (and the members of all other super classes).

Conceptually, you can think of the memory layout of one instance of custom class looking like this:

+============+
|Members of  |
|Object      |
+------------+
|Members of  |
|other super |
|classes     |
|  ...       |
+------------+
|Members of  |
|Custom class|
+============+

Essentially, there will be one block of memory allocated with the size of the custom class (which includes `Object` and all other super classes), and by calling the constructors for each of the super classes, the members of the super classes will be initialized.

See also

- @The New Idiot's link to the JLS for an official reference.

- Java Objects Memory Structure for some additional information.

Problem

I am aware that for every class initialization, each class will extends the object class. Does this mean the JVM will create an object for a custom class and the `Object` class? Can anyone explain this process of initialization of class very clearly. Edit : So if I extend any super class to subclass, does this super class occupies the same memory of subclass?

Original source

Related problems