Java : How to swap two objects [Problem Solved]

As you might knowJava does not support pointers, thus swapping things around could create problems.  In this post I will show an example of swapping two objects.

We have Employee class that holds information about Employees and SwapDemo3 class that will actually swap two employees inside a swap() method.

Swap() method will be using set and get methods of Employee class to get the private data from it.

So here is Employee Class we will be working with:

// an example on Java objects

// constructor
// instance variables, static variable
// methods: getters and setters
// keywords: this, new

public class Employee {

	public static int numberOfEmployees;

	private int   employeeID;
	private String employeeName;
	private String address;
	private String startingYear;

	public Employee( int id, String name, String addr, String year) {

		employeeID = id;
		employeeName = name;

		setAddress( addr );

		startingYear = year;


	public Employee() { this(10000, null, null, null); };

//Set methods
	public void setAddress( String address ) {

		this.address = address;

	public void setEmployeeID(int employeeID) {this.employeeID = employeeID;}
	public void setEmployeeName(String employeeName) {this.employeeName = employeeName;}
	public void setStartingYear(String startingYear) {this.startingYear = startingYear;}

//Get methods

	public String getAddress() { return address; }

	public int getEmployeeID() {return employeeID;}
	public String getEmployeeName() {return employeeName;}
	public String getStartingYear() {return startingYear;}

	public String toString() {

		String s;

		s =   "*****************************************n" +
		"         Employee Information        *n" +
		"*****************************************n" +
		"ID: " + employeeID + "n" +
		"Name: " + employeeName + "n" +
		"Address: " + address + "n" +
		"Starting Year: " + startingYear + "nn";

		return s;

	public static void main( String args[] ) {

		Employee phil, george;

		phil = new Employee();
		System.out.println( phil );
		System.out.println( "t number of employees: " + Employee.numberOfEmployees +"n" );

// Part 2
		phil = new Employee( 10023, "Phil Coulthard",
			"70 The Pond, Seneca@York", "2002");

		george = new Employee( 10089, "George Farr",
			"678, IBM Toronto Lab", "1980" );

		System.out.println( phil );
		System.out.println( george );

		System.out.println( "t number of employees: " + Employee.numberOfEmployees );

// a special case about the default constructor:
// The Java compiler creates the default constructor if
// does not have *any* constructors

and here is SwapDemo3 class that swaps 2 employees using get and set methods of the Employee class.

// A demo on pass-by-value in Java

public class SwapDemo3 {
	public void swap( Employee a, Employee b ) {
		int employeeId;
		String employeeName;
		String address;
		String startingYear;
		employeeId = a.getEmployeeID();
		employeeName = a.getEmployeeName();
		address = a.getAddress();
		startingYear = a.getStartingYear();
	public static void main(String[] args) {
		SwapDemo3 sd = new SwapDemo3();
		Employee one = new Employee( 12345, "John Doe", "70 The Pond", "1999" ),
		two = new Employee( 98765, "Mary Green", "45 King Street", "2007");
		System.out.println( one.toString() + two.toString() );
		System.out.println( "...swap swap swap" );
		sd.swap( one, two );
		System.out.println( "...back in main" );
		System.out.println( one );
		System.out.println( two );

Program output is:

firstuser@firstuser:~/Desktop/JAC444/Week1$ java SwapDemo3
*           Employee Information        *
ID: 12345
Name: John Doe
Address: 70 The Pond
Starting Year: 1999

*           Employee Information        *
ID: 98765
Name: Mary Green
Address: 45 King Street
Starting Year: 2007

...swap swap swap
...back in main
*           Employee Information        *
ID: 98765
Name: Mary Green
Address: 45 King Street
Starting Year: 2007

*           Employee Information        *
ID: 12345
Name: John Doe
Address: 70 The Pond
Starting Year: 1999


Check out more posts on “Useful Java Functions and Tutorials”

Thanks for installing the Bottom of every post plugin by Corey Salzano. Contact me if you need custom WordPress plugins or website design.

Anatoly Spektor

IT Consultant with 6 years experience in Software Development and IT Leadership. Participated in such projects as Eclipse IDE, Big Blue Button, Toronto 2015 Panam Games.

Join the Discussion

Your email address will not be published. Required fields are marked *


  1. Janey Smith

    Thanks for the info, been trying to figure that out for a while now:)

    1. Anatoly Spektor

      Glad you liked it 🙂

  2. Rakesh

    Good info. Like it. I am also trying if there is a way without using those getters and setters, we can swap the complete objects using Pass by reference…… 🙂

  3. pegasus

    Thanks! It’s useful

  4. deepak

    What if the number of attributes for a class is huge. Like, Employee class has attributes- employeeID;

    startingYear, managername, salary, joindate,…..and many more such attributes. so swapping each variable will be tough. I think it can be done using cloning in java. or singleton classes . can u pls show how it can be done

  5. ilian

    What about swapping two elements from ArrayList or List? How to do it? Looks tedious compared to C/CPP!

  6. Deeann Wentland

    Hi there, You have done an excellent job. I will certainly digg it and personally suggest to my friends. I’m sure they will be benefited from this website.

  7. Shoaib Murtaza

    you are not swaping the objects but you are manipulating the objects

  8. Glen

    This is fragile. Adding an instance variable to Employee breaks the “swap” utility without warning. Afterwards, the code will run but will never copy the new member. Even Employee temp = a; a = b; b = temp; does essentially the same thing and works even if the classes change.

    This post should be renamed “How to Copy Member Variables Between Instances” or similar.