# AP® Computer Science A 1.13 Object Creation and Storage (Instantiation)

Source: https://lockincs.com/ap-csa/unit-1/1-13-object-creation-and-storage-instantiation/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 1.13.A, 1.13.A.1, 1.13.A.2, 1.13.A.3, 1.13.B, 1.13.B.1, 1.13.C, 1.13.C.1, 1.13.C.2, 1.13.C.3, 1.13.C.4

An object is created with the keyword new followed by a call to one of the class's constructors, which have the same name as the class. Java picks the constructor whose signature matches the arguments in number, order and type, and passes the arguments by call by value. A reference variable holds the new object's reference, or null if there is no object.

## What you will learn

- 1.13.A Identify, using its signature, the correct constructor being called.
- 1.13.B Develop code to declare variables of the correct types to hold object references.
- 1.13.C Develop code to create an object by calling a constructor.

## 1\. Constructors and constructor signatures

A class contains **constructors** that are called to create objects. They have the same name as the class 1.13.A.1.

A **constructor signature** is the constructor’s name (the same as the class name) plus the ordered list of parameter types. In the constructor header, the parameter list gives the type and the variable name of each value passed in 1.13.A.2.

Constructors are **overloaded** when a class has several constructors with different signatures 1.13.A.3. The `Plant` class used on these pages has 2 constructors:

| Constructor header | Signature | What it does |
| --- | --- | --- |
| `public Plant(String n)` | `Plant(String)` | name `n`, height starts at 0 |
| `public Plant(String n, int h)` | `Plant(String, int)` | name `n`, height `h` |

Java picks the constructor by comparing the argument types with the parameter types, in order:

```
Plant p = new Plant("Fern", 30); // calls Plant(String, int)
Plant q = new Plant("Mint");     // calls Plant(String): height 0
System.out.println(p.getHeight());
System.out.println(q.getHeight());
```

Output:

```
30
0
```

Two constructors with the same parameter types, such as `Box(int side)` and `Box(int len)`, do not compile, because their signatures are the same (`Box(int)`):

```
// does not compile
public class Box
{
    private int size;
    public Box(int side) { size = side; }
    public Box(int len) { size = len; }
}
```

Real view:

```
error: constructor Box(int) is already defined in class Box
```

## 2\. Variables that hold object references

A variable of a reference type holds an object reference or, if there is no object, `null` 1.13.B.1. The variable’s type is the class name.

```
Plant r = null;                 // r refers to no object
System.out.println(r);
r = new Plant("Sage", 8);       // now r refers to a Plant
System.out.println(r.getName());
```

Output:

```
null
Sage
```

## 3\. Creating an object with new

An object is typically created with the keyword `new` followed by a call to one of the class’s constructors 1.13.C.1. Parameters allow constructors to accept values that set the initial values of the object’s attributes 1.13.C.2.

Reading `Plant p = new Plant("Fern", 30);` part by part:

| Part | Meaning |
| --- | --- |
| `Plant` (first) | the class: the reference type of the variable |
| `p` | the variable that will hold the reference |
| `=` | assignment: store the reference in `p` |
| `new` | the keyword that creates the object |
| `Plant("Fern", 30)` | the constructor call: same name as the class; it runs, then control returns here |
| `"Fern", 30` | the arguments, copied into the parameters `n` and `h` |

## 4\. Constructor arguments

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. Arguments are passed using **call by value**: the parameters are initialized with copies of the arguments 1.13.C.3. An `int` argument can go to a `double` parameter, because the `int` is widened to a `double` (topic 1.5).

```
public class Ticket
{
    public Ticket(String event, double price)
    {
        System.out.println(event + " ticket: " + price);
    }

    public Ticket(double price)
    {
        System.out.println("General ticket: " + price);
    }

    public Ticket(int seat, String event)
    {
        System.out.println(event + " seat " + seat);
    }
}

// in main
Ticket a = new Ticket("Gala", 40);
Ticket b = new Ticket(15.5);
Ticket c = new Ticket(12, "Gala");
```

Output:

```
Gala ticket: 40.0
General ticket: 15.5
Gala seat 12
```

`new Ticket("Gala", 40)` has a `String` then an `int`. Only `Ticket(String, double)` has a `String` first, and `40` is widened to `40.0`.

**Watch out:** order matters. `new Plant(30, "Fern")` does not compile, because no `Plant` constructor takes an `int` first:

```
// does not compile
Plant f = new Plant(30, "Fern");
```

Real view:

```
error: incompatible types: int cannot be converted to String
```

The only `Plant` constructor with two parameters is `Plant(String, int)`, and its first parameter needs a `String`, not the `int` 30.

## 5\. Flow of control in a constructor call

A constructor call interrupts the sequential execution of statements: the program first runs the statements in the constructor. Once the last statement in the constructor has run, the flow of control returns to the point immediately after the constructor call 1.13.C.4.

Here a version of the `Plant` constructor also prints:

```
public class Plant
{
    private String name;
    private int height;

    // a version of the Plant constructor that also prints
    public Plant(String n, int h)
    {
        name = n;
        height = h;
        System.out.println("Made " + n);
    }

    public int getHeight()
    {
        return height;
    }
}

// in main
System.out.println("Start");
Plant f = new Plant("Fern", 30);
System.out.println(f.getHeight());
```

Output:

```
Start
Made Fern
30
```

`Start` prints first. The constructor call then runs every statement in the constructor (printing `Made Fern`), and control comes back to the next line, which prints 30.

## Common mix-ups

- Not true: You can call a constructor like a method, without new. An object is created with the keyword new followed by a constructor call: new Book("Dune", 412). Book("Dune", 412) on its own does not compile.
- Not true: Constructor arguments can be in any order. Arguments must be compatible with the parameter types in number and order. If the constructor is Plant(String n, int h), then new Plant(30, "Fern") does not compile.
- Not true: Plant p; creates a Plant. It only declares a variable that can hold a Plant reference. No object exists until new runs. Plant p = null; says clearly that p refers to no object yet.

## In 30 seconds

- Constructors are called to create objects and have the same name as the class.
- A constructor signature is the constructor name plus the ordered list of parameter types; several signatures means overloaded constructors.
- A reference variable holds an object reference, or null if there is no object.
- Create an object with new followed by a constructor call; parameters set the initial values of the attributes.
- Arguments must match the parameter types in number and order and are passed by call by value.
- A constructor call runs every statement in the constructor, then control returns right after the call.

## Checkpoint: 4 practice questions

### Question 1

**Ticket constructors**

| Constructor |
| --- |
| Ticket(String event, double price) |
| Ticket(double price) |
| Ticket(int seat, String event) |

A `Ticket` class has the three constructors shown. Which `Ticket` constructor is called by `new Ticket("Gala", 40)`?

- A. `Ticket(String event, double price)`
- B. `Ticket(double price)`
- C. `Ticket(int seat, String event)`
- D. None; the call does not compile, because 40 is not a double.

**Answer: A.** The arguments are a String then an int. Only (String, double) has a String first, and the int 40 is widened to a double. C has the types in the wrong order; B takes one argument. D is wrong because an int argument can be passed to a double parameter.

### Question 2

**Plant class (implementations not shown)**

| Constructor or method | What it does |
| --- | --- |
| public Plant(String n) | Sets the name to n and the height to 0 |
| public Plant(String n, int h) | Sets the name to n and the height to h |
| public String getName() | Returns the name |
| public int getHeight() | Returns the height |
| public void grow(int cm) | Adds cm to the height |

Which statement declares a variable that can hold a `Plant` reference but refers to no object yet?

- A. `Plant p = new Plant("");`
- B. `Plant p = new Plant("None", 0);`
- C. `String p = null;`
- D. `Plant p = null;`

**Answer: D.** `null` means no object. A and B create objects (one with an empty name, one named None). C has the wrong type for a `Plant` reference: a String variable cannot hold a Plant.

### Question 3

A `Book` class has one constructor, `Book(String title, int pages)`. Which statement correctly creates a `Book` object?

- A. `Book b = new Book(412, "Dune");`
- B. `Book b = Book("Dune", 412);`
- C. `Book b = new Book("Dune", 412);`
- D. `Book b = new Book("Dune");`

**Answer: C.** Use `new` plus a constructor call with arguments in the right number, order and types. A has the arguments in the wrong order. B leaves out the keyword `new`. D passes one argument, but the only constructor needs two.

### Question 4

Consider the following class and code segment in a different class. What is printed?

```java
public class Lamp
{
    public Lamp()
    {
        System.out.print("on-");
    }
}

// in main
System.out.print("A-");
Lamp x = new Lamp();
System.out.print("B-");
Lamp y = new Lamp();
```

- A. `A-B-on-on-`
- B. `A-on-B-on-`
- C. `on-on-A-B-`
- D. `A-B-`

**Answer: B.** Each constructor call interrupts the sequence, runs the constructor's statements, then returns right after the call, so the output is A-on-B-on-. A runs the constructors at the end. C runs them first. D forgets that creating an object runs the constructor.

## Questions students ask

**What is a constructor in Java?**

A constructor is called to create an object of a class. It has the same name as the class, and its parameters let it set the initial values of the object's attributes.

**How do you create an object in Java?**

Use the keyword new followed by a call to one of the class's constructors, and store the reference in a variable of the class type: Plant p = new Plant("Fern", 30);.

**What are overloaded constructors?**

A class has overloaded constructors when it has several constructors with different signatures, such as Plant(String n) and Plant(String n, int h). Java picks one by matching the arguments.

**What does null mean for an object variable?**

null means the reference variable is not associated with any object. Plant r = null; declares a variable that can hold a Plant reference but refers to no object yet.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
