Article

Object Oriented Concepts in Java - Part 1

Page: 1 2 3 4 5 6 7 Next

Copying Objects

There is a difference between the way Java handles basic data like integer and Boolean values and the way Java handles Objects. Consider the following code fragments:

1  int a = 5;    
2  int b = a;    
3  b = b + 1;    
4  System.out.println("a=" + a + " b=" + b);    
5      
6  Tree t1 = new Tree();    
7  t1.height = 5;    
8  Tree t2 = t1;    
9  t2.height = t2.height + 1;    
10 System.out.println("t1.height=" + t1.height +    
11                    " t2.height=" + t2.height);

Now, in both cases we are creating two variables, assigning the value of the first to the second, then changing the value of the second; however, the output of the above code is the following:

a=5 b=6
t1.height=6 t2.height=6

In the case of the integers, changing the value of the second variable did not affect the value stored in the first variable. In the case of the Trees, however, changing the height of the Tree stored in t2 appears of have also changed the height of the Tree stored in t1! Depending on how you think, one or the other of these outcomes probably seems to make sense, but either way there seems to be something very different going on in each of these two cases.

In the first case, which makes sense to most people at first glance, when the value of a is stored in b on line 2 Java actually creates a copy of the integer value 5 to store in b. So in fact we are dealing with two different copies of the number 5: one stored in a, and one stored in b.

The story is very different in the case of Objects. On line 8 when the value of t1 is stored in t2 we are not actually creating a copy of the Tree Object to store in t2! Instead, t2 is made to reference the very same instance of the Tree class in memory. In other words, t2 and t1 are made to point to the exact same Tree! This is why changing the height of t2 also changes the height of t1 -- t2 and t1 aren't separate like a and b were.

So, what to do if you want to create an actual copy of an object? Well, there are several options. The first is to actually create two trees and then just copy the height of one into the height of the other:

1  Tree t1 = new Tree();    
2  t1.height = 5;    
3  Tree t2 = new Tree();    
4  t2.height = t1.height;    
5  t2.height = t2.height + 1;    
6  System.out.println("t1.height=" + t1.height +    
7                    " t2.height=" + t2.height);

This will produce the desired output of "t1.height=5 t2.height=6", because the heights of the trees are in fact basic data values (integers), not Objects, so setting the value of t2's height to equal t1's height creates a separate copy of the integer value for t2 to use.

Another option is to implement a special method in your class for creating copies. This method should supply a direct copy of the object for separate use. Here's what Tree class would look like with the copy method added:

1  class Tree {    
2    public int height = 0;    
3    public void grow() {    
4      height = height + 1;    
5    }    
6    public Tree copy() {    
7      Tree copy = new Tree();    
8      copy.height = height;    
9      return copy;    
10   }    
11 }

And here's how you'd use this new method:

1  Tree t1 = new Tree();    
2  t1.height = 5;    
3  Tree t2 = t1.copy(); // Create a copy of t1    
4  t2.height = t2.height + 1;    
5  System.out.println("t1.height=" + t1.height +    
6                    " t2.height=" + t2.height);

Again, this will produce the desired result.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links