One Object, with Changing Data

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:

How many Point objects are created by this program?     One — the object referenced by pt

How many temporary String objects are created by this program?     Two — one temporary String object for each println()

One Object, with Changing Data

The program changes the data inside a Point object using that object's move() method:

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

    pt.move( -13, 49 ) ;             // change the x and y in the Point
    System.out.println( pt ); 
  }
}

Here is a picture of this:

changing object contents

QUESTION 13:

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