Holding the State of an Object

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:

Is the Car class made up of smaller software objects?

  • No. It consists of some primitive (non-object) data and one method.

What are the instance variables of class Car?

  • startMiles
  • endMiles
  • gallons

What is the method of class Car?

  • calculateMPG()

Holding the State of an Object

The state of an object consists of the values held by its instance variables. These may change during the lifetime of the object. Here is a main() program that constructs a Car object.

class MpgTester
{
  public static void main ( String[] args )
  {
    Car myCar = new Car( 12000, 12340, 12.3 );

    . . . . . . 
  }
}

When the object is constructed, its state is initialized to:

  • startMiles = 12000
  • endMiles = 12340
  • gallons = 12.3

The object referenced by myCar holds these values as long as it exists.


QUESTION 3:

Could a collection of several cars be regarded as an object?