This is a Java Program to Interchange Two Employee Objects by Passing them to Swap() Method.
Here we make a swap method to interchange the information of two employees using Employee Objects by passing them to swap() method.
Here is the source code of the Java Program to Interchange Two Employee Objects by Passing them to Swap() Method. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
public class Swap_Demo
{
static String emp1, emp2;
Swap_Demo()
{
emp1 = "this is A";
emp2 = "this is B";
}
void swap(String x,String y)
{
String res;
res = x;
x = y;
y = res;
System.out.println("Employee1: "+x);
System.out.println("Employee2: "+y);
}
public static void main(String[] args)
{
Swap_Demo obj = new Swap_Demo();
System.out.println("Before swapping:");
System.out.println("Employee1:"+emp1);
System.out.println("Employee2:"+emp2);
System.out.println("After swapping:");
obj.swap(emp1,emp2);
}
}
Output:
$ javac Swap_Demo.java $ java Swap_Demo Before swapping: Employee1:this is A Employee2:this is B After swapping: Employee1: this is B Employee2: this is A
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Apply for Java Internship
- Check Java Books
- Check Programming Books
- Apply for Computer Science Internship
- Practice BCA MCQs