spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / java / beginning / chap5 / 2 To page 1To page 2To page 3To page 4To page 5center
[previous]

Beginning Java 2 SDK 1.4 Edition

Subject Matter Expert - Managed Services (PA)
Next Step Systems
US-PA-Wayne

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


Duplicating Objects Using a Constructor

When we were looking at how objects were passed to a method, we came up with a requirement for duplicating an object. The need to produce an identical copy of an object occurs surprisingly often.

Java provides a clone() method, but the details of using it must wait for the next chapter.

Suppose you declare a Sphere object with the following statement:

Sphere eightBall = new Sphere(10.0, 10.0, 0.0);

Later in your program you want to create a new object newBall, which is identical to the object eightBall. If you write:

Sphere newBall = eightBall;

this will compile OK but it won't do what you want. You will remember from our earlier discussion that the variable newBall will reference the same object as eightBall. You will not have a distinct object. The variable newBall, of type Sphere, is created but no constructor is called, so no new object is created.

Of course, you could create newBall by specifying the same arguments to the constructor as you used to create eightBall. In general, however, it may be that eightBall has been modified in some way during execution of the program, so you don't know that its instance variables have the same values--for example, the position might have changed. This presumes that we have some other class methods that alter the instance variables. You could fix this by adding a constructor to the class that will accept an existing Sphere object as an argument:

// Create a sphere from an existing object
Sphere(final Sphere oldSphere) {
  radius = oldSphere.radius;
  xCenter = oldSphere.xCenter;
  yCenter = oldSphere.yCenter;
  zCenter = oldSphere.yCenter;
}

This works by copying the values of the instance variables of the Sphere object, passed as an argument, to the corresponding instance variables of the new object.

Now you can create newBall as a distinct object by writing:

Sphere newBall = new Sphere(eightBall);  // Create a copy of eightBall

home / programming / java / beginning / chap5 / 2 To page 1To page 2To page 3To page 4To page 5center
[previous]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: July 1, 2002
Revised: July 1, 2002


URL: http://webreference.com/programming/java/beginning/chap5/2/6.html