Dangerously Similar Program

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

Can a constructor be used to change the data inside an object?

Answer:

No. Constructors always create new objects. (They might get data from an old object, but a completely separate object will be constructed using a different chunk of main memory.)

Dangerously Similar Program

Here is the example program, this time modified to create a second Point object:

import java.awt.*;
class ChangingData2
{
  public static void main ( String arg[] )
  {
    Point pt = new Point( 12, 45 );                  // construct a Point
    System.out.println( pt );     

    pt       = new Point( -13, 49 ) ;                // construct a new Point
    System.out.println( pt ); 
  }
}

Here is a picture showing the situation before and after the second assignment statement:

new object created

In the "After" picture, the first object is shaded to emphasis that it is now "garbage." The reference variable pt refers to the newly created object.

QUESTION 14:

What will this second version of the program output to the monitor?