Lesson 1.13: Object Creation and Storage (Instantiation) | AP CSA

Unit 1 · Lesson 1.13 · Code Mechanics

Lesson 1.13: Object Creation and Storage (Instantiation)

Reading time: 9–11 min·Practice: 8 exercises·Mastery: applied scenario

What you'll learn in this lesson

  • 1.13.A. Identify the correct constructor being called based on its signature, including overloaded constructors.
  • 1.13.B. Declare variables of the correct types to hold object references, including using null.
  • 1.13.C. Create objects using new, pass constructor arguments (call-by-value), and explain how execution flow interrupts for the constructor body.

This lesson is part of the AP CSA Course and Exam Description (effective May 2027).

AP exam weight: Instantiation appears in nearly every FRQ. You must write new ClassName(args) with correct argument types and order. See the Oracle Java tutorial on constructors for further reading.

Constructors: creating objects with new

A constructor has the same name as the class, no return type, and is called with new to create and initialize an object.

Instantiation Pattern

ClassName variable = new ClassName(arguments);

new allocates heap memory. The constructor initializes attributes. The reference is stored in variable.

Constructor signatures and overloading

Constructor Signature = Name + Parameter Types

Box()           // no-arg constructor
Box(int size)   // one-arg constructor: overloaded

Arguments must match parameters in number, order, and compatible type. Call-by-value applies — parameters get copies.

AP Trap: execution flow through constructor

When new is called, execution jumps into the constructor body. The calling code pauses until the constructor completes, then the new object reference is returned.

AP Trap: null vs object

Box b = null; holds no object. Calling b.size throws NullPointerException at run time.

▶ Java Code Editor
Try It Yourself
Write real Java, hit Run, and your code executes on a live compiler. Output is checked automatically.
Tier 2 · AP Practice

Practice Questions

Which correctly creates a Scanner to read from the keyboard?
A. Scanner sc = Scanner(System.in);
B. Scanner sc = System.in;
C. sc = new Scanner(System.in);
D. Scanner sc = new Scanner(System.in);
D. Object creation needs: type + variable name + new + constructor call. A is missing new. B assigns a stream not a Scanner. C is missing the type declaration.
A class has constructors Item() and Item(int value). Which call invokes the no-arg constructor?
A. new Item(0)
B. new Item()
C. new Item(null)
D. Item()
B. new Item() matches no-arg signature. A calls Item(int) with 0. C is a compile error (int can't accept null). D is missing new.
Constructor is Point(int x, int y). Which call causes a compile error?
A. new Point(3.0, 4.0)
B. new Point(3, 4)
C. new Point(3, (int) 4.9)
D. new Point(-1, 0)
A. 3.0 and 4.0 are doubles. Narrowing to int without a cast is a compile error. B uses int literals (valid). C explicitly casts (valid). D uses negative int (valid).
What is printed?

Box a = new Box(10);
Box b = a;
a.size = 99;
System.out.println(b.size);
Are a and b the same object or different objects?
A. 10
B. 0
C. 99
D. NullPointerException
C. b = a aliases the same object. a.size = 99 modifies the shared object. b.size = 99.
Which statement about constructors is NOT true?

I. A constructor has the same name as the class.
II. A constructor must have return type void.
III. Constructors can be overloaded.
A. I only
B. II only
C. I and III only
D. II and III only
B. Constructors have NO return type at all, not even void. I and III are both true. Only II is false.
When new ClassName(args) is called, which describes execution flow?
A. The constructor runs in parallel with calling code.
B. Calling code continues; the constructor runs later.
C. Execution stays in calling code; the constructor never runs.
D. Execution jumps into the constructor body, runs completely, then returns the new object reference to the calling code.
D. Constructor calls interrupt sequential execution. The constructor runs fully, then control returns with the new object reference.
What is printed?

Box box = new Box(5);
resize(box, 20);
System.out.println(box.size);
Does modifying b.size inside resize affect box.size in the caller?
A. 20
B. 5
C. 0
D. NullPointerException
A. b receives a copy of the reference pointing to the same Box. b.size = 20 mutates the shared object. box.size is now 20. Mutating an object's state through a parameter IS visible to the caller -- unlike reassigning a primitive.
Which correctly declares a Dog variable that intentionally holds no object?
A. Dog d = new Dog();
B. Dog d = 0;
C. Dog d = null;
D. Dog d = "";
C. null is the correct placeholder for a reference variable with no object. A creates an object. B and D are type mismatches -- compile errors.
PRACTICE WITH A GAME — CHOOSE ONE:

Output Predictor — Object Creation

Predict the exact output. Tap below if you need the class.

Question 1 of 6Score: 0

Bug Hunt — Instantiation

Each snippet has exactly one buggy line. Click it, then submit.

Bug 1 of 7Caught: 0

Tier 3 · AP Mastery Challenge

The Bank Account

Assume:

class BankAccount {
    String owner;
    double balance;
    BankAccount(String name, double amount) {
        owner = name; balance = amount;
    }
    BankAccount(String name) {
        owner = name; balance = 0.0;
    }
}

Part A

Which constructor is called by new BankAccount("Alice")?
A. Two-parameter constructor, balance auto-set to 0.0.
B. One-parameter constructor: owner="Alice", balance=0.0.
C. Compile error -- two-parameter constructor must be used.
D. No-arg constructor.
B. One String argument matches BankAccount(String). Sets owner="Alice", balance=0.0.

Part B

What does b2.balance hold after this code?

BankAccount b1 = new BankAccount("Bob", 500.0);
BankAccount b2 = b1;
b1.balance = 750.0;
A. 500.0
B. 0.0
C. NullPointerException thrown.
D. 750.0
D. b2 = b1 aliases the same object. b1.balance = 750.0 modifies the shared object. b2.balance is 750.0.

Part C: Structured response

Write a third overloaded no-arg constructor for BankAccount that sets owner to "Unknown" and balance to 0.0. Then explain why three constructors with the same name are allowed.

Extension · Beyond the Exam

The this keyword in constructors

Inside a constructor, this refers to the object being created. When a parameter has the same name as an instance variable, this.name = name disambiguates: this.name is the field; name is the parameter. You will use this pattern extensively in Unit 3.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]