Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

What does a object references on stack actually look like?

shankernaikJun 24 2017 — edited Jun 24 2017

As per the documentation :

In some of Sun's implementations of the Java virtual machine, a reference to a class instance is a pointer to a handle that is itself a pair of pointers:

one to a table containing the methods of the object and

a pointer to the Class object that represents the type of the object, and

the other to the memory allocated from the heap for the object data.

So given,  two classes A and B as follows :

  1. class A{ 
  2.    void m1(){ 
  3.    System.out.println(" from m1 method "); 
  4.    } 
  5.  
  6. class B extends A { 
  7.    void m2(){ 
  8.    System.out.println(" from m2 method "); 
  9.    } 

In main method I make following objects, what will their bit pattern refer to? I have commented my understanding, correct me if I am wrong.

A a1 = new A();

// a pointer to dispatch table of a1 contains only method m1.

// a pointer to the memory allocated from the heap for the object data.

// a pointer to Class class object of A


B b1
= new B();

// pointer to dispatch table of b1 contains method m1 and m2.

// a pointer to the memory allocated from the heap for the object data.

// a pointer to Class class object of B

A a2 = new B();

// What will the dispatch table of a2 be?

// a pointer to the memory allocated from the heap for the object data.

           // Which Class class object will it point, A or B ?

Thanks in advance.

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 22 2017
Added on Jun 24 2017
1 comment
248 views