title: How to get the reference value of an object of a class in Java?
body: <pre><code>public class Box {
  
      int height;
      int length;
      int width;
  
  }
  
  
  
  public class Volume {
  
      public static void main(String args[]){
  
          Box mybox = new Box();
          Box mybox1= mybox;
          Box mybox2 = new Box();
  
  
          System.out.println(" The address of the mybox object is:"+System.identityHashCode(mybox));
          System.out.println(" The value of the mybox object is:"+mybox);
  
          System.out.println(" The address of the object mybox1 is :"+ System.identityHashCode(mybox1));
          System.out.println(" The value of the mybox1 object is:"+mybox1);
  
  
          System.out.println(" The address of the object mybox2 is :"+ System.identityHashCode(mybox2));
          System.out.println(" The value of the mybox2 object is:"+mybox2);
  
      }
  }
  </code></pre>
  
  <p>Goal of this program is to help me understand the concepts of Object creation and memory allocation in Java. Based on my understanding, the new operator shall create a memory for an object and return the reference stored in the memory. In order to get the address of the object, I am using the System.identityHashCode function. In order to get the value of the reference stored in that memory address, I am directly printing the object. Below we can see the results.</p>
  
  <pre><code> The address of the mybox object is:1956725890
  
   The value of the mybox object is:Box@74a14482
  
   The address of the object mybox1 is :1956725890
  
   The value of the mybox1 object is:Box@74a14482
  
   The address of the object mybox2 is :356573597
  
   The value of the mybox2 object is:Box@1540e19d
  </code></pre>
  
  <p>Based on the above results, my understanding is that mybox and mybox1 will have the same address and they will carry the same reference value in that address as mybox1 was not instantiated and was assigned to mybox. mybox2 shall have a new address and it will have a new reference value in that address.</p>
  
  <p>So my question is </p>
  
  <ul>
  <li><p>Without instantiating, how would mybox1 have an address and a reference in that address ?</p></li>
  <li><p>The book Java for complete reference does mention that every object after instantiation shall hold the memory address of the actual object Box.So why is the value of the reference stored in the mybox2 different from the value of the reference stored in mybox ? I am assuming that just by printing the variable, it would give me the value stored in that variable, which is the reference to the object Box.</p></li>
  </ul>
  
  <p>Thank you people for any suggestions that can help me understand the very basic concepts of java. I am a newbie and I really appreciate the wealth of knowledge spread by stackoverflow members. </p>