5

Lets say I have a class A.java,

enter image description here

When I will execute a constructor method of A, it will create a memory space for xyz Object.

A xyz = new A();

The reference to memory may be something like,

[xyz] ---> '0x34524'

Thats basics of OOP. Simple enough!

Now,

What happens if a class is inheriting from different parent classes? How many object space will be created in memory?

Lets say we have,

enter image description here

and then we create an object of class D.java,

D omg = new D();

Here as we know that D's object will call construct of C.java and so on until A.java. Does this mean that in memory we are having 4 different memory reference, because we are instantiating all of the four objects (one directly and another 3 indirectly)?

[omg] ---> '0x34525'
[C]   ---> '0x34526'
[B]   ---> '0x34527'
[A]   ---> '0x34528'

Note :

  1. This isn't homework question, this is just a curiosity question.
  2. I am aware of the fact that if we have a instance variable inside an A.java then we will not create only object A but we will be creating other internal object whenever we hit new keyword.
Community
  • 1
  • 1
TeaCupApp
  • 11,316
  • 18
  • 70
  • 150

3 Answers3

5

First, a tid bit... calling the constructor of an object does not allocate it. In bytecode, the initialization new Object() is expressed as something to the effect of...

new java/lang/Object
invokespecial java/lang/Object <init>()V

The new instruction takes care of allocating the space and acquiring a reference to the yet uninitialized object, while the invokespecial handles calling the constructor itself (which is internally compiled to a void method named <init>, hence the descriptor <init>()V).

Moving on, internals of object allocation and representation on the heap are entirely JVM specific. However, as far as I know, there is only one allocated object per allocated object, no matter its number of super classes. The object itself in memory has space for the instance fields of both its own class and its super classes. It must also have space for a virtual method table, in order to do virtual dispatch when performing virtual method calls (e.g. via invokevirtual) for the object.

Internally, the Oracle HotSpot JVM manages things called oops, or ordinary object pointers. You can read more about the HotSpot memory layout here. Feel free to browse the HotSpot source repository.

obataku
  • 29,212
  • 3
  • 44
  • 57
  • An object's fields are resolved statically, so you can know at compile time exactly how big the full object would be, as well as the offsets for each of its fields. Moreover, each override of a given method always uses the same field offset, since fields are statically resolved. So I don't know of any reason why the JVM would instants ate four objects instead of one. – yshavit Aug 07 '12 at 00:28
  • The compiler does not resolve where fields are in memory, the JVM does... the Java allocates one object in memory and executes the code from the super class constructors from the immediate super class up to `Object` (inside the constructor, it is possible other objects are constructed too e.g. for field initialization). – obataku Aug 07 '12 at 00:44
1

JVM allocates memory for only one object (here D)

  1. memory allocation and initialization happens bottom(here D) to top(Object)
  2. initialization/calling constructors happens Top(Object) to Bottom(here D)

reference :

http://www.artima.com/designtechniques/initialization.html

0

I have not read this anywhere but its my experience. When you call new D(), the constructor chain begins, it first creates an java.lang.Object and then extends it to an A, I mean after creating the Object (which is root of all objects), A is initialized on it, by adding memory for A's members, including fields and methods (which are a pointer to some code!). And then it extends to B and so on.

In the process of extension if a method is overriden, the method pointer in the object will point to new code.

It will be only one reference to D.

Amir Pashazadeh
  • 7,170
  • 3
  • 39
  • 69