May 24, 2019
Hot Topics:

Object Integrity & Security: Duplicating Objects

  • May 2, 2007
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

There is no actual object created here. Note that there is no instantiation—you only create a reference here. To experienced developers, this may seem obvious; however, when teaching objects to students for the first time, this is a major area of confusion. No actual spot object has been created. However, you still can use the spot reference; you just need to understand what you can use it for.

For example, consider the following operation. You can actually assign the spot reference to the fido reference.

spot = fido;

Here is the important concept: Although you can assign these references to the same object, you are not copying anything. In this case, there is still only a single object despite the fact that you have two references, even though this might not be obvious. Take a look at Listing 02.

// Class Dog
class Dog {

   String name;
   String breed;

   public Dog(String n, String b) {

      name  = n;
      breed = b;


   public String getName() {

      return name;


   public String getBreed() {

      return breed;



// Class Duplicate
public class Duplicate {

   public static void main(String[] args) {

   Dog fido = new Dog("fido", "retriever");

   Dog spot;

   spot = fido;

   System.out.println("name = " + fido.getName());
   System.out.println("name = " + spot.getName());


Listing 02: The Duplicate Class with Two References

You have added code to the application to illustrate the concepts just explained. In Listing 02, you create the spot reference and then include a print statement to verify that both the fido and the spot reference point to the same object. When you run the updated application, you can see in Figure 02 that both the fido and the spot references print out the same name, in this case the original fido object.

Figure 02: Simple Object Application With Two References

You can see the graphical representation in Diagram 02. Both of the references are actually pointing to the fido object despite the fact that one of the reference is called spot.

Diagram 02: An Object with Two References.

One of the issues here is that, if you change the name for one of the references, you change them for both. Add some code to change the name of spot. Take a look at the additional lines of code below.

// Class Dog

   public void setName(String n) {

      name = n;



// Class Duplicate




When you run this code, you can see in Figure 03 that both of the object references are accessing this same object—whose name was changed to rover.

C:column34>"C:Program FilesJavajdk1.5.0_07binjava"
            -classpath . Duplicate
name = rover
name = rover

Page 2 of 4

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date