Instantiating the Point Objects

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:

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

How many reference variables are there?    3

How many objects are there?    Zero

Instantiating the Point Objects

Here is a picture of the variables just as the program starts running. No objects have been instantiated yet, so the reference variables a, b, and c do not refer to any objects. To emphasize this, a slash has been put through the box for each variable.

three reference variables a, b, c

Here is the program again:

import java.awt.*;
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"
  }
}

The program:

  1. Declares three reference variables a, b, and c, which may refer to objects of type Point.
  2. Instantiates a Point object with x=0 and y=0.
    (The documentation tells us that a constructor without parameters initializes x and y to zero.)
  3. Saves the reference to the object inthe variable a.
  4. Instantiates a Point object with x=12 and y=45.
  5. Saves the reference to the object in the variable b.
  6. Instantiates a third Point object.
  7. Saves the reference in the variable c.

Once each Point object is instantiated, it is the same as any other (except for the values in the data). It does not matter which constructor was used to instantiate it.

QUESTION 5:

What are the values of x and y in the third point?