Alex Rivera | Logout

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

Asked 2013-07-23T07:32:10.083
11

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?

Edit
Report

1 Answer

9

It doesn't create 2 objects, it only creates one object of custom class . The super class constructor is called to initialize the fields of the super class as by definition the super class must be initialized to construct the sub class. But remember the created object is an instance of both the custom class as well as the java.lang.Object.

Read JLS 12.5 to know how it works. A snippet :

Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden

Suggested Reading:

Does invoking a constructor mean creating object?

answered 2013-07-23T07:34:48.087

Your Answer