我有一个代码如下,我在调用中交换Instance variables
类Function
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";
}
}
现在代码的输出如下
Name1 Name2
Name1 Name2
我不明白的是我交换了值obj1 and obj2
- 对他们的引用。之后classes
我正在更改值。
为什么代码没有生效。
谢谢您的回复