Picture of Main Memory

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 objects are in the system the very instant the program starts running?
    • Zero.
    • (This is always the answer to this question.)
  • How many objects are in the system just before the program stops running?
    • One, the object referenced by car.
    • (The String object in the println has already become garbage.)

Picture of Main Memory

picture of main memory

 

When a program starts running there are no objects, just class definitions and a static main() method. Usually main() then constructs some objects and calls their methods to do the work of the program. In our example, main() constructs just one object and then calls its calculateMPG() method.

The picture shows the variable car in the static main() method referring to the object that has been constructed following the class definition of Car. The static main() method is part of the MilesPerGallon class.

In the picture, the class definitions are shown as pink clouds as a reminder that they are not full objects. The object in this picture is shown as a solid rectangle.

QUESTION 11:

Could several objects of type Car be constructed?