Multiple Constructors

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

The different constructors require different parameters. (Different types of data are supplied to the constructors.)

Multiple Constructors

Any of the three constructors can be used to create a Point. It is a matter of convenience which constructor you use. Here is an example:

import java.awt.*;      // import the package that contains Point
class PointEg1
{

  public static void main ( String arg[] )
  {
    Point a, b, c;              // reference variables

    a = new Point();            // create a Point at (0,  0); 
                                // save the reference in "a"
    b = new Point( 12, 45 );    // create a Point at (12, 45); 
                                // save the reference in "b"
    c = new Point( b );         // create a Point containing data equivalent
                                // to the data referenced by "b"
  }
}

To use the definition of class Point, import the package that contains it. The statement:

import java.awt.*

says to use the AWT package that comes with Java. The * says that everything defined in the package can be used (although this program only uses the Point class).

QUESTION 4:

Say that the program has just been loaded into main memory and is just about to start running.

How many reference variables are there?

How many objects are there?