public class Student{
private String name;
public Student(String name)
{this.name=name;
}
public void setName(String sett){
name=sett;}
public String getName()
{return(name);}
}
public class Swaptest{
public static void swap(Student p,Student q){
Student temp;
temp=p;
p=q;
q=temp;
}
public static void main(String a[])
{Student s1=new Student("John");
Student s2=new Student("Mary");
swap(s1,s2);
System.out.println("s1="+s1.getName());
System.out.println("s2="+s2.getName());
}
}
Output:s1=John
s2=Mary
Why is the code not swapping names??
bigzos 0 Light Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.