Kevin's Blog

IT, Java, Web, Ubuntu

Java Uses Call By Sharing

I just saw [Java: It is most certainly pass by value].
It is very shocking that I can still often see people saying “Java is call by value”. I mean, I don’t really get why it is even controversial. I think it is clear that “Java is call by value”. Oh, actually it is more precisely “call by sharing” (Notice that it is NOTcall by reference“).

Call-by-reference means a reassigned reference to a method parameter variable is visible by the caller of the method which means if a new object reference is assigned to the parameter variable in a method then caller will have the newly assigned one after execution of the method whereas call-by-sharing means the new reference is not visible by the caller. Thus the old reference will still remain after calling the method. That means if a new reference is assigned to the parameter variable, it is visible just within the method thus after execution of the method, the caller will still get the old reference so the same old object.

The people who do not understand the difference between call-by-reference and call-by-sharing do probably not know the difference between assigning a new reference to a variable and changing the state of an object the reference of which is stored in the variable. I guess, it is presumably caused by their ignorance about a reference of object and the actual object. They perhaps think a variable is an object itself or that contains an object, yet the variable does not contain an object but the reference of an object. So when we use a variable to change the state of an object or use the methods in it, we are not directly having the actual object but are using the reference of the object stored in the variable in order to access the object.

To sum up, Java uses call-by-sharing NOT call-by-reference. The difference between call-by-reference and call-by-sharing is

call-by-reference: You are able to assign a new reference to a method parameter variable in a method, and it will be visible by the caller of the method so after calling the method, the caller will get the new reference (new object).

call-by-sharing: You are able to assign a new reference to a method parameter variable in a method, BUT it will NOT be visible by the caller of the method so after calling the method, the caller will still get the same old reference which was passed as a method parameter to the method.

In both evaluation strategies, you have access to the reference so are able to change the state of the object the reference of which is stored in the parameter variable if the object is mutable. Therefore it is possible to change the values of the object member variables in the method and these changes are visible by the caller but the caller still see the same old object with a different state from the state it had before calling the method.

Loading Facebook Comments ...
Loading Disqus Comments ...

6 Comments

  1. Can you plz explain with a example..

  2. Grzegorz Warywoda

    19 May, 2011 at 9:55 pm

    //Example for “call by sharing”

    class MyObject{
    public int x=666; //;]
    }

    void fun(MyObject inner){
    inner.x = 10; //Working on reference to ‘outer’ object

    //!!!!!!!!Reference to object ‘outer’ will be disposed only in scope of this method !!!!
    inner= new MyObject(); //create new reference.
    nner.x = -1; //Working on new local reference
    }

    MyObject outer = new MyObject();
    fun(outer);
    print( outer.x ); //10

  3. Hi, Please execute the following code, and please let me know in what category it will come.

    public class Main {
    
        public static void main(String[] args) {
            Main m = new Main();
            Xyz x = new Xyz();
            x.y = 10;
            System.out.println(x.y);
            m.show(x);
            System.out.println(x.y);
    
    
        }
    
        public void show(Xyz x) {
            System.out.println(x.y);
            x.y = 15;
            System.out.println(x.y);
    
        }
    }
    
    class Xyz {
    
        public int y;
    }
    

    The out put comes like this,

    10
    10
    15
    15
    

    As per your explanation, if we change in called method, the values wont change in Caller method.

    then the out put should be 10 , 10 , 15 ,10

    why it came differently … Please explain.

    • Hello, Sekhar

      What your code does is changing object state which means it assigns a new value to the field (the object member variable) y.

      So

      m.show(x);
      

      is ‘Call By Sharing’ as it passes an object x (it is indeed the object reference of the class Xyz stored in the variable x) to the show() method.
      Then what the show() method does is changing the state of the x.

      x.y = 15;
      

      This is changing the object state by reassigning a new value to the variable y. Notice that you did not pass the variable y in the object x but the object itself.

      So if your show() method does like this

      public void show(Xyz x)
      {
        System.out.println(x.y);
        x = new Xyz();
        x.y = 15;
        System.out.println(x.y);
      }
      

      You will get

      10
      10
      15
      10
      

      as you can re-assign a new object reference of the new instance of the type Xyz to the parameter variable x but this is not visible by the caller, the main() method, so after calling the show() method, x will still contain the reference of the first Xyz object you created before calling the show() method, and it has 10 in the variable y.

      I am planing to put much better examples for this topic yet just have no time at the moment. My company is about to launch a new web application so I am, just like others, really busy. :)
      I’ll put the examples as soon as possible.

      For your information, you can use [code lang=”java”][/code] to put Java source code here.
      [code lang=”java”]
      some java code here
      [/code]

      and the plain text with the fixed-width font for
      [code lang=”plain” gutter=”false”]
      [/code]

      Regards,
      Kevin

  4. Hi, this post has really been helpful to me to understand the difference between passing methods. Thanks for that.

    My question is : had we been using call-by-reference in the given example then would the following be correct?

    //Example for “call by reference” 
    class MyObject{
      public int x=666; 
    }
    void fun(MyObject inner){
      inner.x = 10; //Working on reference to ‘outer’ object
      //!!!!!!!!Reference to object ‘outer’ will be tvisible by the caller of the method 
      inner= new MyObject(); //create new reference.
      inner.x = -1; //Working on reference visible to caller
    }
    MyObject outer = new MyObject();
    fun(outer);
    print( outer.x ); //-1
    

    TIA

    • Hi, Pratik

      No, you can’t use call-by-reference in Java as Java doesn’t have it.
      If you run the code snippet you put here, you will get 10 but NOT -1.

      void fun(MyObject inner){
        inner.x = 10; // Working on the reference to the instance of MyObject which is assigned to the variable 'outer’.
      
        inner= new MyObject(); // create a new reference, and it is NOT visible by the caller.
        inner.x = -1; // It will never be visible by the caller.
      }
      

Leave a Reply

© 2014 Kevin's Blog

Theme by Anders NorenUp ↑

%d bloggers like this: