Wednesday, 24 October 2018

Java Objet and Object Reference Variable

  • An object is chunk of memory , and the reference to the object is way to reach up to that object in the memory
  • Object reference variable contains address of the object which is declared in the heap memory
  • Class Box { double height; double width; double depth; } Box b1; //declare reference ..it will be null b1 = new Box(); //Create an instance assign to reference variable b1 Box b2 = b1; //Only creates reference to this object.. will not create object memory..use same object memory..
  • Now I  am going to set some property in b1
  • b1.height = 10; b1.width = 20; b1.depth = 30;
  • Then I printed b2 properties
  • System.out.println(b2.height); System.out.println(b2.width); System.out.println(b2.depth); //output 10 20 30
  • I got same values for b2, because it using same memory but different reference

No comments:

Post a Comment

Search This Blog

Contact us

Name

Email *

Message *