January 31, 2015
Hot Topics:

Object Integrity & Security: Duplicating Objects

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


This series, The Object-Oriented Thought Process, is intended for someone just learning an object-oriented language and who wants to understand the basic concepts before jumping into the code, or someone who wants to understand the infrastructure behind an object-oriented language he or she is already using. These concepts are part of the foundation that any programmer will need to make the paradigm shift from procedural programming to object-oriented programming.

Click here to start at the beginning of the series.

In keeping with the code examples used in the previous articles, Java will be the language used to implement the concepts in code. One of the reasons that I like to use Java is because you can download the Java compiler for personal use at the Sun Microsystems Web site http://java.sun.com/. You can download the standard edition, J2SE 5.0, at http://java.sun.com/j2se/1.5.0/download.jsp to compile and execute these applications. I often reference the Java J2SE 5.0 API documentation and I recommend that you explore the Java API further. Code listings are provided for all examples in this article as well as figures and output (when appropriate). See the first article in this series for detailed descriptions for compiling and running all the code examples.

The code examples in this series are meant to be a hands-on experience. There are many code listings and figures of the output produced from these code examples. Please boot up your computer and run these exercises as you read through the text.

For the past several months, you have been exploring various issues regarding object integrity, security, and performance. In this month's article, you continue with this theme and begin to learn how objects are duplicated. This is not a trivial issue. Although copying primitives such as numbers is mostly straightforward, copying objects is a more complicated task.

Copying Objects

One of the interesting areas of object integrity pertains to the fact that developers often do not know what they have; in many cases, this can cause significant problems. Duplicating objects falls into this category. The problem is that objects are processed using pointers and references. Duplicating a pointer or a reference does not necessarily duplicate the entire object. The main issue here is that objects are often made up of other objects—and these relationships are critical to consider.

Referencing the Object

Start with a simple example. Suppose that you have two classes, the actual application (with main) called Duplicate.java and a standalone class called Dog.java. The only action that this Duplicate application initially performs is to create a Dog object and print out its name, which you assign in the constructor as 'fido'. Both classes are presented in Listing 01.

// 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");

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


Listing 01: The Duplicate Class

By looking at this graphically, you can see how the objects are represented in memory. Diagram 01 shows that the fido reference, defined by the circle, points to the physical memory location of the fido object. Internally, the details are a bit more complicated; however, the graphical representation provides a good way to understand what is happening, and it conveys the important concepts you are concerned about.

Diagram 01: An Object with a Single Reference.

The key to Figure 01 is to understand that when you access the fido object, you are really following a reference to the object. The address location of fido in the following line of code contains the address of the fido object. In many ways, this behavior is much easier to follow when using languages such as C/C++ that allow programmers to actually perform pointer arithmetic.


In short, the 'fido' object in the line above is a reference, not the physical object.

When this application is executed, you get the output in Figure 01. It simply prints out the dog's name.

Figure 01: Simple Object Application

Although this application is very simple, you can quickly illustrate the issue you identified earlier in this article. Now, create a second Dog reference.

Dog spot;

Page 1 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.

Sitemap | Contact Us

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