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 4current pageTo page 6
[previous] [next]

Beginning Java 2 SDK 1.4 Edition

Java Developer (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume
Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans


Try It Out--Multiple Constructors for the Sphere Class

The code for the extra constructors is:

class Sphere {
  // First Constructor and variable declarations
  ...
  // Construct a unit sphere at a point
  Sphere(double x, double y, double z) {
    xCenter = x;
    yCenter = y;
    zCenter = z;
    radius = 1.0;
    ++count;           // Update object count
  }

  // Construct a unit sphere at the origin
  Sphere() {
    xCenter = 0.0;
    yCenter = 0.0;
    zCenter = 0.0;
    radius = 1.0;
    ++count;           // Update object count
  }

  // The rest of the class as before...
}

The statements in the default constructor that set three data members to zero are not really necessary, as the data members would be set to zero by default. They are there just to emphasize that the primary purpose of a constructor is to enable you to set initial values for the data members.

If you add the following statements to the CreateSpheres class, you can test out the new constructors:

public class CreateSpheres {
  public static void main(String[] args) {
    System.out.println("Number of objects = " + Sphere.getCount());

    Sphere ball = new Sphere(4.0, 0.0, 0.0, 0.0);               // Create a sphere
    System.out.println("Number of objects = " + ball.getCount());

    Sphere globe = new Sphere(12.0, 1.0, 1.0, 1.0);             // Create a sphere
    System.out.println("Number of objects = " + Sphere.getCount());

    Sphere eightBall = new Sphere(10.0, 10.0, 0.0);
    Sphere oddBall = new Sphere();
    System.out.println("Number of objects = " + Sphere.getCount());

    // Output the volume of each sphere
    System.out.println("ball volume = " + ball.volume());
    System.out.println("globe volume = " + globe.volume());
    System.out.println("eightBall volume = " + eightBall.volume());
    System.out.println("oddBall volume = " + oddBall.volume());
  }
}

Now the program should produce the output:

Number of objects = 0
Number of objects = 1
Number of objects = 2
Number of objects = 4
ball volume = 267.94666666666666
globe volume = 7234.559999999999
eightBall volume = 4.1866666666666665
oddBall volume = 4.1866666666666665

How It Works

When you create a Sphere object, the compiler will select the constructor to use based on the types of the arguments you have specified. So, the first of the new constructors is applied in the first statement that we added to main(), as its signature fits with the argument types used. The second statement that we added clearly selects the last constructor as no arguments are specified. The other additional statements are there just to generate some output corresponding to the new objects. You can see from the volumes of eightBall and oddBall that they both are of radius 1.

It is the number and types of the parameters that affect the signature of a method, not the parameter names. If you wanted a constructor that defined a Sphere object at a point, by specifying the diameter rather than the radius, you have a problem. You might try to write it as:

// Illegal constructor!!!
  // This WON'T WORK because it has the same signature as the original!!!
  Sphere(double diameter, double x, double y, double z) {
    xCenter = x;
    yCenter = y;
    zCenter = z;
    radius = diameter/2.0;
  }

If you try adding this to the Sphere class and recompile, you'll get a compile-time error. This constructor has four arguments of type double, so its signature is identical to the first constructor that we wrote for the class. This is not permitted--hence the compile-time error. When the number of parameters is the same in two overloaded methods, at least one pair of corresponding parameters must be of different types.

Calling a Constructor from a Constructor

One class constructor can call another constructor in the same class in its first executable statement. This can often save duplicating a lot of code. To refer to another constructor in the same class, you use this as the name, followed by the appropriate arguments between parentheses. In our Sphere class, we could have defined the constructors as:

class Sphere {
  // Construct a unit sphere at the origin
  Sphere() {
    radius = 1.0;
    // Other data members will be zero by default
    ++count;                    // Update object count
  }

// Construct a unit sphere at a point
  Sphere(double x, double y, double z)
  {
    this();                    // Call the constructor with no arguments
    xCenter = x;
    yCenter = y;
    zCenter = z;
  }

  Sphere(double theRadius, double x, double y, double z) {
    this(x, y, z);              // Call the 3 argument constructor
    radius = theRadius;         // Set the radius
  }
  // The rest of the class as before...
}

In the constructor that accepts the point coordinates as argument, we call the default constructor to set the radius and increment the count of the number of objects. In the constructor that sets the radius, as well as the coordinates, the constructor with three arguments is called to set the coordinates, which in turn will call the constructor that requires no arguments.


home / programming / java / beginning / chap5 / 2 To page 1To page 2To page 3To page 4current pageTo page 6
[previous] [next]


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger

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


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