0 Replies Latest reply on Jun 23, 2017 11:33 AM by shankernaik

    What does a object references on stack actually look like?

    shankernaik

      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 :

       

       

      class A{
         void m1(){
         System.out.println(" from m1 method ");
         }
      }
      
      class B extends A {
         void m2(){
         System.out.println(" from m2 method ");
         }
      }

       

       

      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.