View RSS Feed

My Java Tips

Object references

Rate this Entry
by , 11-21-2011 at 04:58 PM (737 Views)
In this post, I will be talking about how to refer to an object. I will present a simple example to make things obvious.


We have a class called Student. It had 2 attributes, getter setter methods and a method named showAll() to show the contents of both the attributes.

Java Code:
public class Student {

	private int id;
	private String name;

	public Student(int id, String name) {
		this.id = id;
		this.name = name;
	}
	public Student() {
		// TODO Auto-generated constructor stub
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public void showAll()
	{
		System.out.println("id: " + id);
		System.out.println("name: " + name);

	}
}
So far so good. Now lets do some experiments.


// main method
Java Code:
Student obj1 = new Student(1, "Laiq");

System.out.println("From main: ");
obj1.showAll();

testMe(obj1);

System.out.println("From main: ");
obj1.showAll();


	public static void testMe(Student a)
	{
		System.out.println("From testMe: ");
		a.showAll();

		a.setName("Toni");

		System.out.println("From testMe: ");
		a.showAll();

	}
Output:


From main:
id: 1
name: Laiq
From testMe:
id: 1
name: Laiq
From testMe:
id: 1
name: Toni
From main:
id: 1
name: Toni




Now lets try to understand what happened in the example given in Object references (I). We created an object called obj1 of class Student using constructor with 2 parameters. When we called a method called testMe() which takes an object of class Student as argument. In Java, objects are passed by reference. So when we displayed the contents of object a (in testMe) method, we got the same contents as of obj1. No worries until now.

In testMe(), we altered the name attribute of object a, and displayed the contents, we got the altered contents. No problem so far. When we displayed the contents of object obj1 in main method, we noticed that the contents of obj1 has been changed. How this happened???

Actually the objects are passed by reference in methods. So object obj1 and object a, both are referring to the same.

Now lets do one more experiment. Review the code below:

Java Code:
Student obj1 = new Student(1, "Laiq");

System.out.println("obj1: ");
obj1.showAll();

Student obj2 = new Student(2,"Dave");

System.out.println("obj2: ");
obj1.showAll();

obj1 = obj2;
obj1.setName("Toni");

System.out.println("obj1: ");
obj1.showAll();

System.out.println("obj2: ");
obj2.showAll();
Output:


obj1:
id: 1
name: Laiq
obj2:
id: 1
name: Laiq
obj1:
id: 2
name: Toni
obj2:
id: 2
name: Toni

The output shows interesting results. One you use equal operator to assign one object to another object, then both refer to the same object in memory, but with different name.



Just to make thing more obvious, let me give a very interesting example:

Java Code:
Student obj1 = new Student(1, "Laiq");
Student obj2 = new Student(2,"Dave");

System.out.println("HashCode obj1: " + obj1.hashCode());
System.out.println("HashCode obj2: " + obj2.hashCode());

obj1 = obj2;

System.out.println("HashCode obj1: " + obj1.hashCode());
System.out.println("HashCode obj2: " + obj2.hashCode());
Output:


HashCode obj1: 26022015
HashCode obj2: 3541984
HashCode obj1: 3541984
HashCode obj2: 3541984

Its clear that obj1 and obj2 were two different objects with different set of data. But, when we assigned obj2 to obj1 using equal to operator, both started referring to the same set of data (object in memory).

I hope this will clear your concepts.

Happy coding.

Submit "Object references" to Facebook Submit "Object references" to Digg Submit "Object references" to del.icio.us Submit "Object references" to StumbleUpon Submit "Object references" to Google

Comments