From the free course AP Computer Science A on Lykke

1.13 Object Creation and Storage

Key concepts: Object creation and storage (instantiation) · Constructors · Constructor names matching class names · Constructor signatures · Overloaded constructors · Reference-type variables · The new keyword · Calling a class constructor · Adding a newly created object to an ArrayList · Using the remainder operator (%)

A Java variable of a reference type does not contain an object directly; it holds a reference to an object, or the special value null when it refers to no object.

1.13 Object Creation and Storage

A Java variable of a reference type does not contain an object directly; it holds a reference to an object, or the special value null when it refers to no object. Creating the object and storing its reference are separate steps that Java combines in a familiar pattern.

Declaring a reference and creating an object

The declaration names the type of object the variable may reference. The keyword new then creates an object by calling one of the class’s constructors, and the assignment stores the resulting reference in the variable.

Essential Knowledge 1.13.B.1: A variable of a reference type holds an object reference or, if there is no object, null.

Essential Knowledge 1.13.C.1: An object is typically created using the keyword new followed by a call to one of the class’s constructors.

Competitor finalist;
finalist = new Competitor();

The first statement declares a variable. At that moment, it does not yet identify a Competitor object. The second statement constructs a new Competitor object and assigns its reference to finalist.

The shorter combined form performs both actions:

Competitor finalist = new Competitor();

A reference can later be reassigned. Reassignment changes which object the variable identifies; it does not copy one object into another.

Competitor first = new Competitor();
Competitor second = first;
first = new Competitor();

After these statements, second still refers to the original object, while first refers to the newly created object. Both variables briefly referred to the same object, but assigning a new reference to first did not change second.

Misconception check: Competitor second = first; does not create a second Competitor. It copies the reference. To create another object, use new again.

Constructors and constructor signatures

A constructor is the special code used when an object is created. Its name must exactly match the class name, and it has no return type—not even void. Constructor parameters allow the new object to receive initial attribute values.

Essential Knowledge 1.13.C.2: Parameters allow constructors to accept values to establish the initial values of the attributes of the object.

public class Competitor {
    private String name;
    private int score;

    public Competitor() {
        name = "Unknown";
        score = 0;
    }

    public Competitor(String n, int s) {
        name = n;
        score = s;
    }
}

The constructor signature consists of the constructor name and the ordered list of parameter types. In this class, the signatures are Competitor() and Competitor(String, int). The parameter names n and s are not part of the signature.

Essential Knowledge 1.13.C.3: A constructor argument is a value passed into a constructor when the constructor is called. Arguments must be compatible, in both number and order, with the constructor’s parameter types.

Competitor a = new Competitor();
Competitor b = new Competitor("Riley", 42);

These constructors are overloaded because the class has multiple constructors with different signatures. Java selects the constructor whose parameter list matches the arguments in the call.

Misconception check: Constructors are not overloaded merely because they have different parameter names. Competitor(String name, int score) and Competitor(String n, int s) have the same signature.

Storing objects in collections

A reference variable can be stored in an ArrayList, allowing a collection to hold many object references. The declared element type must match the kind of reference being stored.

ArrayList<Competitor> competitorList =
    new ArrayList<Competitor>();

Competitor newcomer = new Competitor("Jordan", 18);
competitorList.add(newcomer);
competitorList.add(new Competitor("Kai", 21));

The second add call creates a new Competitor and immediately stores its reference in competitorList. This is equivalent in effect to creating the object first, assigning it to a variable, and then adding that variable.

A collection can also be declared for another reference type:

ArrayList<Match> matches = new ArrayList<Match>();

Here, matches is a local ArrayList<Match> variable. It can store references to Match objects, not arbitrary objects such as Competitor.

If a program needs to test whether a collection’s size satisfies a condition, the remainder operator % may be applied to competitorList.size(). For example, competitorList.size() % 2 == 0 tests whether the size is even; the operator does not create or store objects.

AP skills in action

Computational Thinking Practice 2 — Develop Code is demonstrated by declaring a correctly typed reference, choosing a constructor, supplying compatible arguments, and storing the resulting object in a collection. Computational Thinking Practice 3 — Analyze Code is required when tracing which object each reference identifies after assignments, constructor calls, and add operations.

Retrieval check: What does new Competitor("Ari", 10) produce, and what does competitorList.add(...) store? The expression produces a newly constructed Competitor object and add stores its reference in the list.

1.13 Object Creation and Storage - AP Computer Science A - image 1
1.13 Object Creation and Storage - AP Computer Science A - image 1
1.13 Object Creation and Storage - AP Computer Science A - diagram 1
1.13 Object Creation and Storage - AP Computer Science A - diagram 1

Source Materials

The whole course on Lykke · Browse all public course wikis