Skip to content
Marlbridge

AP® Computer Science A · Unit 1 · Topic 1.13

Object Creation and Storage (Instantiation): Study Guide (Computer Science A 1.13)

Learn how an object is created with the keyword new and a constructor, how constructor signatures and overloading select the right constructor, and what a reference variable holds before and after.

Making an object

In Topic 1.12 you declared a variable such as Box b; and saw that this creates no object. So where do objects come from? From the class itself. A class contains constructors, which are called to create objects. A constructor has the same name as the class, and, unlike a method, it has no return type.

An object is typically created with the keyword new followed by a call to one of the class’s constructors. Here is a class Box that the examples will use. It has two attributes (width and height) and three constructors. (Methods in a class are called instance methods, Topic 1.14; for now notice only the constructors.)

class Box {
    private int width;
    private int height;

    public Box() {
        width = 1;
        height = 1;
    }

    public Box(int side) {
        width = side;
        height = side;
    }

    public Box(int w, int h) {
        width = w;
        height = h;
    }

    public int area() {
        return width * height;
    }

    public String describe() {
        return width + " x " + height;
    }
}

Each constructor does the same kind of job: set the starting values of the attributes. They differ in what information the caller supplies.

Constructor signatures and overloading

A constructor signature consists of the constructor’s name, which is the same as the class name, and the ordered list of parameter types. The parameter list in the header of a constructor lists the types of the values that are passed and their variable names.

Constructor header Signature
public Box() Box()
public Box(int side) Box(int)
public Box(int w, int h) Box(int, int)

Constructors are overloaded when there are multiple constructors with different signatures. This is the same idea as overloaded methods in Topic 1.9: the compiler chooses by the number, order and types of the arguments.

Creating objects and storing references

public class MakeBoxes {
    public static void main(String[] args) {
        Box a = new Box();
        Box b = new Box(4);
        Box c = new Box(3, 5);
        Box d = null;
        System.out.println(a.describe() + " | " + b.describe() + " | " + c.describe());
        System.out.println(a.area() + b.area() + c.area());
    }
}
1 x 1 | 4 x 4 | 3 x 5
32

Take Box c = new Box(3, 5); apart. There are three actions:

  1. Declare a variable c of the reference type Box.
  2. Create the object: new Box(3, 5) calls the constructor Box(int, int), which sets width to 3 and height to 5.
  3. Store the resulting object reference in c by assignment.

A variable of a reference type holds an object reference or, if there is no object, null. In the program, d is deliberately set to null: it refers to no box. (Calling a method on d would cause a NullPointerException, Topic 1.14.)

Variables and the Box objects they refer to Variable boxes on the left, object boxes on the right. Objects: Box object with width = 1, height = 1; Box object with width = 4, height = 4; Box object with width = 3, height = 5. Arrows and values: variable a refers to the Box object object; variable b refers to the Box object object; variable c refers to the Box object object; variable d holds null and refers to no object. Box a Box b Box c Box d null Box object width = 1 height = 1 Box object width = 4 height = 4 Box object width = 3 height = 5
Figure 1. Four variables. The variables a, b and c refer to three different Box objects, each with the width and height its constructor set. The variable d holds null and refers to no object.

Arguments and call by value

The parameters of a constructor let it accept values to establish the initial values of the object’s attributes. A constructor argument is a value passed into a constructor when it is called. The arguments must be compatible in order and number with the types in the parameter list of the constructor signature. As with methods, arguments are passed by call by value: the parameters are initialized with copies of the arguments.

If no constructor matches, the compiler reports it. For the Box class:

Statement Fault
new Box(1, 2, 3) no constructor has three parameters
new Box("x") a String cannot be passed to the int parameter
new Box(2.5) a double cannot be passed to an int parameter

The flow of control

A constructor call interrupts the sequential execution of statements, so the program first executes the statements in the constructor before continuing. Once the last statement in the constructor has been executed, the flow of control returns to the point immediately following where the constructor was called.

class Note {
    public Note(String text) {
        System.out.println("creating " + text);
    }
}

public class Order {
    public static void main(String[] args) {
        System.out.println("before");
        Note n = new Note("A");
        System.out.println("after");
    }
}
before
creating A
after

The message from inside the constructor appears between the two lines printed by main, because new Note("A") runs the constructor to its end before main continues.

Constructors in the library

Library classes are created with constructors too, and you have already used several. The Java Quick Reference lists them next to the methods.

  • new Scanner(System.in) calls a Scanner constructor with one argument, the source to read from.
  • new String("hello") calls the String constructor String(String str), which constructs a new String object that represents the same sequence of characters as str. (Topic 1.15 explains that a string literal such as "hello" also creates a String object, so for strings you rarely need new.)
  • new ArrayList<String>() calls the ArrayList constructor that takes no arguments and creates an empty list; you will use it in Unit 4.

The reading skills are identical to those for your own classes: the class name tells you the type of object, the argument list selects the constructor, and the variable on the left must be declared with the class type (or a type the course says is compatible).

What storage means

The topic title says object creation and storage. Two different things are stored. The object, with all of its attribute values, is stored in the computer’s memory when the constructor runs. The reference to that object is what gets stored in the variable. That is why the same object can be reached through more than one variable (Topic 1.12), and why an object that no variable refers to any longer can no longer be used by the program.

You can check your understanding by predicting what each of these statements does to the diagram:

  1. Box a = new Box(2, 3); creates one new object and makes a refer to it.
  2. Box b = a; creates no new object; b now refers to the same one.
  3. a = new Box(9); creates another object and makes a refer to it; b still refers to the first object.
  4. b = null; makes b refer to nothing. The first object is now referred to by no variable.

Each statement changes what a variable holds; only statements that contain new ever create an object. A diagram with variables on the left, objects on the right and an arrow for each reference makes this process visible at every step, and drawing one is the most reliable way to avoid mistakes on exam questions about references.

A routine for any constructor call

Exam questions on this topic usually ask one of three things: which constructor runs, what the attributes hold afterwards, or why a call does not compile. The same short routine answers all three.

  1. List the constructors. Write each signature down from the class: here Box(), Box(int) and Box(int, int).
  2. Evaluate the arguments. Work out the value and the type of each argument, in order, left to right. An argument can be a literal, a variable, an expression or a method call.
  3. Pick the matching signature. The number must be equal, and each argument’s type must fit the parameter in the same position (an int fits a double parameter; a double does not fit an int). If none fits, the code does not compile. If two fit, the more exact match is used.
  4. Run the constructor. Create a table of the parameters and the attributes. The parameters hold copies of the arguments. Follow each statement of the constructor in order, and note what it stores in the attributes.
  5. Return and store. The constructor ends, control returns to the point after the call, and the new object’s reference is stored where the statement says: in a variable, or passed on as an argument.

A few traps are worth knowing. If a constructor has a parameter with the same name as an attribute, it takes care to tell the two apart (Topic 3.9 explains how); in this topic’s classes the names differ, so there is no confusion. If a class has no constructor with parameters but you pass arguments, the call does not compile. And if two constructors need the same type of information in a different order (for example (int, double) and (double, int)), they are different signatures, and the order of your arguments decides which is called.

Worked example 1: Which constructor, and what do the attributes hold?

Task. Using the Box class above, state which constructor each statement calls and give the values of width and height.

(a) Box p = new Box(); (b) Box q = new Box(6); (c) Box r = new Box(2, 7); (d) Box s = new Box(r.area()); (assume r is the box from (c))

(a) No arguments: Box(). Width 1, height 1.

(b) One int argument: Box(int). Both attributes are set to the side: width 6, height 6.

(c) Two int arguments: Box(int, int). Width 2, height 7.

(d) The argument r.area() is evaluated first, before the constructor is called. The area of r is 2 * 7 = 14, an int, so Box(int) is called with 14: width 14, height 14.

Check. In every case, the number and type of the arguments select the constructor. Part (d) shows that an argument may itself be a method call, which runs first.

Worked example 2: Constructor parameters are copies

Task. A class Wallet has a constructor that adds a bonus to the starting value. State what is printed.

class Wallet {
    private int coins;

    public Wallet(int start) {
        start = start + 5;
        coins = start;
    }

    public int getCoins() {
        return coins;
    }
}

public class Bonus {
    public static void main(String[] args) {
        int n = 10;
        Wallet w = new Wallet(n);
        System.out.println(n + " " + w.getCoins());
    }
}

Step 1. In main, n is 10. new Wallet(n) calls the constructor Wallet(int).

Step 2. Call by value: the parameter start is initialized with a copy of n, which is 10.

Step 3. Inside the constructor, start = start + 5; makes the copy 15, and coins = start; stores 15 in the object’s attribute. The constructor ends and control returns to main.

Step 4. n was never changed: only its copy was. w.getCoins() returns 15. The output is 10 15.

10 15

Reflection. The two numbers differ because the argument and the parameter are different variables, and because the object’s attribute coins is a third location, set by the constructor.

Common misconceptions

  • “A constructor is a method with a return type of its own class.” A constructor has no return type at all and shares the class’s name.
  • “Box b; makes a box.” It declares a variable; new Box(...) makes the box.
  • “new is optional.” Without it, no object is created.
  • “A constructor with a different parameter name is a different constructor.” The signature is the name and the parameter types. Names do not count.
  • “A constructor changes the argument variable.” Arguments are passed by value; only the copy changes.
  • “A reference variable can never be null.” It can: null means no object.
  • “The statements after new run before the constructor.” The constructor runs completely first.
  • “I can pass any argument to any constructor.” The number, order and types must match one signature exactly (with int allowed for double).

Where this leads

Practise with the Topic 1.13 practice questions, recap with the revision notes and tick off the checklist. The next topic, 1.14 Calling Instance Methods, shows how to use the objects you have just created by calling methods on them.

Quick questions

What is the difference between declaring a variable and creating an object?

Declaring (Box b;) makes a variable that can hold a reference to a Box. Creating (new Box(4)) builds the object by calling a constructor. The usual one-line form Box b = new Box(4); does both: the object is created and a reference to it is stored in b.

How do I know which constructor will be called?

Match the arguments to the constructors' parameter lists by number, order and type. new Box() calls the constructor with no parameters; new Box(4) calls the one with a single int; new Box(3, 5) calls the one with two ints. If no signature matches, the code does not compile.

Does a constructor have a return type?

No. A constructor's header has no return type, not even void, and its name is exactly the name of the class. That is how you recognise it.

Can I create an object without storing it in a variable?

The code new Box(2); compiles as a statement, but the reference is discarded, so you cannot use the object afterwards. In practice you store the reference in a variable or pass it straight into something that uses it.

AP® and Advanced Placement® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website. Marlbridge is an independent tuition provider. It is not an authorized AP course provider, an AP school or an exam centre, and these resources are not official College Board materials. Reading them or doing simulations does not meet the laboratory requirement of an AP science course.