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 + "\n\n";

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”


About Anatoly Spektor

My name is Anatoly Spektor (originally Anatolijs Spektors) I am Software and Web Developer. I have worked in Seneca Center for Development of Open Technology on Big Blue Button Add-on - Polling Module, Red Hat and some other places :) I am an author of the book 'Eclipse Debugging How To', Muay Thai fighter and amateur photographer ;)
This entry was posted in Java, open-source, Useful Functions. Bookmark the permalink.

8 Responses to Java : How to swap two objects [Problem Solved]

  1. Janey Smith says:

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

  2. Rakesh says:

    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 says:

    Thanks! It’s useful

  4. deepak says:

    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 says:

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

  6. 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 says:

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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s