TOPIC 1.12 · STUDY

Objects: Instances of Classes

A class is a reusable blueprint. An object is one instance made from that class, with its own state. On the AP CSA exam, every class sits conceptually under Object. A reference variable stores a reference value; declaring it does not create an object. Assigning one reference to another makes both names refer to the same object.

1.12.A

Classes and objects

A class describes a kind of thing: what attributes each instance can store and what behaviors those instances share. Think of the class as a blueprint. It names the type. It is not one particular copy of that thing sitting in memory.

An object is one instance built from that blueprint. Two objects of the same class can hold different attribute values at the same time. That per-instance data is the object's state. The class can be reused to make as many separate objects as the program needs.

One class box labeled as the GardenBed blueprint sits above two separate instance boxes, northBed and southBed, each with its own soil depth.

One blueprint, two objects. Matching class names do not mean the instances share state.

On the AP CSA exam, every class is conceptually under Object. Object is the root of the class hierarchy. A more specific type is still an Object. For example, a Scooter can be thought of as a more specific Vehicle, and both still sit under Object. Students are not asked to write extends or design that hierarchy in this topic.

Behaviors that belong to a type come from the class definition, including what Object provides at the root. The name of a reference variable only labels which object you are talking about. Renaming through a second variable does not change the object's class.

Key points

  • A class is a blueprint; an object is one instance of that class.
  • Each instance keeps its own state.
  • One class can produce many independent objects.
  • Object is the conceptual root; more specific types are still Objects.
  • Students are not asked to write inheritance code for this topic.

1.12.B

Reference variables

A declaration such as DockSign west; introduces a reference variable named west whose type is DockSign. That statement alone does not create a DockSign object. The variable is a place that can store a reference value. Constructing an object takes a separate step with new.

DockSign west = null;
// west holds null: it refers to no object yet

west = new DockSign(4);
// now west refers to one DockSign object

DockSign also = west;
// also stores the same reference value as west

null is a valid reference value meaning no object. The variable still has its declared type. A reference value either refers to an object or is null; it is not a packed copy of every attribute stuffed into the variable as primitives.

Two reference names, west and also, both point to one DockSign object after assignment copies the reference.

Assignment copies the reference, not a second object. Both names now point at the same instance.

Each new constructs a separate object, even when the starting attribute values match. Reassigning a reference later can make that name point at a different object; other variables that still hold the old reference keep naming the earlier object.

TrailMark a = new TrailMark(2);
TrailMark b = new TrailMark(2);
// a and b are two distinct objects

TrailMark c = a;
a = b;
// c still refers to the first object; a and b share the second

Key points

  • A reference declaration creates a variable, not an object.
  • null means the variable currently refers to no object.
  • new constructs an object and yields a reference to it.
  • Assigning one reference to another aliases the same object.
  • Equal attribute values do not merge two separately constructed objects.

Declaring a reference is not the same as creating an object. DockSign west; only names a variable that can hold a reference. An object appears only when construction (such as new DockSign(4)) runs.

Practice this topic