basic java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sajithamol

    basic java

    When you have an object passed to a method and when the object is reassigned to a different one, then is the original reference lost?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sajithamol
    When you have an object passed to a method and when the object is reassigned to a different one, then is the original reference lost?
    Java passes by value always

    Comment

    • itsraghz
      New Member
      • Mar 2007
      • 124

      #3
      formatted

      Originally posted by sajithamol
      When you have an object passed to a method and when the object is reassigned to a different one, then is the original reference lost?
      As the admin said, "Java passes everything by value". This everything includes even the references to objects.

      Lets say, you have a method which takes an object as a parameter as follows

      [Code=java]
      public void doChange(MyClas s myClassObj)
      {
      //do something..
      }
      [/Code]

      And you call that method from some other piece of code as follows

      [Code=java]
      //calling method
      public void method1()
      {
      MyClass myClassObj = new MyClass();
      doChange(myClas sObj);
      }
      [/Code]

      The reference variable myClassObj is being assigned to an object of class MyClass in Heap. Just like primitive variables holding bit patterns to represent a value, reference variables also store the bit patterns to reach an object.

      Once you call the method doChange() by passing the reference variable "myClassObj " to it, the value of the bit patterns held by "myClassObj " is being copied and passed into the called method doChange(). In the called method doChange(), the copied-and-sent bit pattern is received in the same name as that of the original reference variable "myClassObj ". But the compiler treats that as a different one just like primitives.

      Lets say the received reference variable as "myClassObjLoca l" for easy understanding. In this stage, both the original refernece variable "myClassObj " and the received reference variable inside the doChange() method "myClassObjLoca l" (to compiler) point to the same object in Heap.

      Now if you change the state of the object being pointed by, it will reflect to both the reference variables because both of them point to the same object in Heap. Whereas,if you change the received reference variable "myClassObjLoca l" to point to a new object (reassign a different object), it does NOT reflect back to the original object because the bit pattern to the "myClassObjLoca l" alone gets changed!

      If you are clear with the above paragraphs, lets go to an example to make it more clear.

      Lets take a small example.

      [Code=java]
      class TestObjRef
      {
      int intValue;

      public TestObjRef()
      {
      intValue = 1;
      }

      public static void changeStateOfOb ject(TestObjRef obj)
      {
      obj.intValue = 2;
      }

      public static void changeReference (TestObjRef obj)
      {
      obj = new TestObjRef();
      obj.intValue = 9;
      }

      public static void main(String[] args)
      {
      TestObjRef obj1 = new TestObjRef();
      System.out.prin tln("obj1.intVa lue (1) = "+obj1.intValue );
      changeStateOfOb ject(obj1);
      System.out.prin tln("obj1.intVa lue (2) = "+obj1.intValue );
      changeReference (obj1);
      System.out.prin tln("obj1.intVa lue (3) = "+obj1.intValue );
      }
      }
      [/Code]

      Running the above code produces the following output:

      [Code=java]
      obj1.intValue (1) = 1
      obj1.intValue (2) = 2
      obj1.intValue (3) = 2
      [/Code]

      changeStateOfOb ject Method

      This is because, changeStateOfOb ject() method just changes the value of the variable "intValue" which definitely constitutes the state of object. And as such, both the "obj1" in main() method and "obj" in changeStateOfOb ject() method point to the same object. Means, they both hold the same bit patterns to reach a single object of TestObjRef class in heap.

      That's why the output in the second line shows the changed state of object "2" as the value of "intValue" property.

      changeReference () Method

      If you look at the changeReference () method, by the time of receiving the argument, both the "obj1" reference variable in main() method and received reference variable "obj" in changeReference () method both hold the same bit patterns to reach the same and single TestObjRef class in Heap.

      But inside the method, the received "obj" reference variable is reassigned to a newly created object of TestObjRef class. In this case, only the bit pattern of the "obj" reference variable inside the changeReference () method is changed and the change is NOT reflected back to the originally sent reference variable "obj1".

      That's why the third line in the output still shows the value of "intValue" as "2" since the changed value "9" is only reflected in the received local reference variable "obj" in changeReference () method.

      Hope this helps.

      Note
      The received argument (object reference variable) is treated local to the method. That means, the scope of the variable is only local to the called method just like primitives and the variable cannot be accessed outside the method.
      Last edited by itsraghz; Jul 13 '07, 07:41 AM. Reason: just to highlight a part of reply

      Comment

      • itsraghz
        New Member
        • Mar 2007
        • 124

        #4
        Originally posted by sajithamol
        When you have an object passed to a method and when the object is reassigned to a different one, then is the original reference lost?
        Adding to the previous example, you can have a look at this link for another example of same topic with explanation.

        Comment

        Working...