0

I have a code as below where I am swapping the Instance variables of class in Function called SwapNames

public class Sam3 
{
    public String FirstName, LastName;

    public static void main(String[] args) 
    {   
        Sam3 objSam1 = new Sam3();
        Sam3 objSam2 = new Sam3();

        objSam1.FirstName = "Name1";
        objSam2.LastName  = "Name2";        

        System.out.print(objSam1.FirstName + " " + objSam2.LastName);

        SwapNames(objSam1, objSam2);

        System.out.print(objSam1.FirstName + " " + objSam2.LastName);
    }

    public static void SwapNames(Sam3 obj1, Sam3 obj2)
    {   
        Sam3 temp = obj2; 
        obj2 = obj1;
        obj1 = temp;

        obj1.FirstName = "First Name";
        obj2.LastName  = "Last Name";
    }
}

Now the Output of the Code is as below

Name1 Name2
Name1 Name2

The thing which I dont understand is I swapped the values in obj1 and obj2 - References made to their classes.After that I am changing the value.

Why the code is not taking effect.

Thanks for reply

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
Java Beginner
  • 1,635
  • 11
  • 29
  • 51

3 Answers3

2

In Java everything is pass by value.

When you call SwapNames(objSam1, objSam2); reference objSam1 and objSam2 will be copied to the method SwapNames parameter obj1 and obj2.

so after swap reference value of obj1 and obj2 will be swapped not objSam1 and objSam2.

objSam1 and objSam2 is still holding the old reference Object. So that does not effect anything.


For better clarification

Sam3 temp = obj2; 
obj2 = obj1;
obj1 = temp;

If you print the value of FirstName & LastName of the Object.

System.out.print(obj1.FirstName + " " + obj2.LastName);

you will see result will be Name2 Name1

As because obj1 is now referencing the second Obeject and obj2 is referencing first object.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • Thanks for Reply. If I put objSam1.FirstName = "Name2" with out swapping its taking effect.But why If I do a swap and change the value it is not taking effect – Java Beginner Feb 19 '13 at 18:45
1
public static void SwapNames(Sam3 obj1, Sam3 obj2)
{   
    Sam3 temp = obj2; 
    obj2 = obj1;
    obj1 = temp;

    obj1.FirstName = "First Name"; //<-- is actually objSam2 
    obj2.LastName  = "Last Name";  //<-- is actually objSam1
}

to see what's going on:

class Test{

    public int i=2;
}

public class Main {
public static void main(String[] args) {

    Test t1 = new Test();
    t1.i=0;

    Test t2 = new Test();
    t2.i=1;


    System.out.println(t1.i+" "+t1);
    System.out.println(t2.i+" "+t2);

    test(t1,t2);

    System.out.println(t1.i+" "+t1);
    System.out.println(t2.i+" "+t2);



}

public static void test(Test a, Test b){
    Test swap=a;
    a=b;
    b=swap;

    System.out.println(a.i+" "+a);
    System.out.println(b.i+" "+b);
}

}
/*
output:
0 Test@3c56b64c
1 Test@60da5686
1 Test@60da5686
0 Test@3c56b64c
0 Test@3c56b64c
1 Test@60da5686
*/
El Hocko
  • 2,581
  • 1
  • 13
  • 22
0

In your code, there are two Sam3 object reference variable
which contains object of Sam3 class.

for objSam1 first Instance of Same3 [remember Sam3
contains two instance variable FirstName, LastName]
objSam1.FirstName = "Name1";
objSam1.LastName = null; // this is default value if you don't set or initialize

for objSam2 which is second Instance of Sam3 [ remember Sam3
class contains two instance variable FirstName, LastName]
objSam2.FirstName = null; //this is default if we don't initialize .FirstName
objSam2.LastName = "Name2";

Now after calling swaping method objects are changed but you are changing

objSam2.FirstName = "First Name";
//previously FirstName was null and there is no change in .LastName (see code above)

and for
objSam1.LastName = "Last Name";
// previously it was null and there is no change in .FirstName (see above code)

So the computer is giving correct answer.

AmitG
  • 10,365
  • 5
  • 31
  • 52