Pass By Reference

Does Java pass the objects by reference?

Though it seems to be correct, its a misconception.

In C++, its true. But in Java, still the object is being passed by value which is nothing but the value of the references! The reference variables of the object are copied and in turn sent to the called method (as that of any primitive variable) and thus it is pass-by-value!

A good reference for the same would be :

JavaRanch Story at ->

Another useful info at ->

A good example is (taken from JavaRanch query:

public class Test14
    static String s ="Instance"; //Line 1
    public static void method(String s)
       s+="Add";  //Line 4
   public static void main(String a[])
       Test14 t = new Test14();   
       s = "New Instance";  //Line 2
       String s = "Local";  //Line 3

What is output?

A1 Local Instance
A2 Local New Instance
A3 Loca Add New Instance
A4 Compiler Error
The ouptut is A2. (Local New Instance).


Line 1:Here, the class level static "s" is initialized with the value "Instance" once the class is loaded

Line 2:The same class level static "s" is reinitialized with the value "new Instance" in the main method

Line 3:A local String variable "s" which has the same name as that of class level variable is declared inside the main() method and its given the value "Local"

Once the local variable "s" is passed on to the method method() [remember! -> The local variable takes preference than that of anything else inside the method!], the value of the reference variable "s" (local s) is copied and passed to the method() method.

Line 4:
The value of the reference variable is received in the same name as that of the static class level String variable "s" in the method method(). This is tricky as it may be confusing of shadowing!. But whatever you do inside the method, its reflected only to the local variable inside the method's scope and the change is NOT reflected back to the called method!. For the easy use, lets call the "s" variable inside the method() method as "localS" (it will be that way to the compiler!).

Since because the copy of the value of reference variable is passed, at present both the "s" variables (one in the called method [method()] ie., "localS" and another one in the calling method [main()]) will be pointing to the same object in the Heap.

Rule to remember :
If you do any change in the actual object being pointed, the change will be reflected to both the references. In this manner, you change the object being pointed by. But not the object reference! That means, you can change the object pointed by the "s" in main but you can NOT make "s" in main() to point to a new object!.

Till line 4, you have,

"s" -> pointing to the object with the content "Local".
"localS" -> same as that of "s"

Once line 4 is executed,

"s" -> no change!
"localS" -> "Local Add"

since you change the content with "Add" appended, and as a result of "+" concatenation operator it returns a new object, that new object is assigned to the reference variable "localS". Since "localS" is local only to the method() method, only the "localS" is affected and NOT the original "s" in main.

Once, the program flow goes back to the called method main(),
you have the local "s" variable unaffected by method() whose value is "Local" and the class level static variable was also not unaffected with the value "New Instance" (the latest updated value in main()).

When you access the values of local "s" and static "s" through its object "t.s". In that case, the value gets printed is :

New Instance
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.