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