1.13 Object Creation and Storage (Instantiation)
Create objects with new and constructors, store their references in variables, and predict what happens when two variables or a method share the same object.
1. Reference Guide
| Term | Definition / Example | College Board Requirement |
|---|---|---|
| Instantiation | Creating an object from a class with new: Dog myDog = new Dog("Rex"); |
Unit 1, Topic 1.13, p. 46 |
| Constructor | Code with the same name as the class that sets up a new object: public Dog(String name) |
Unit 1, Topic 1.13, p. 46 |
| Overloaded constructors | Several constructors with different parameter lists: Dog() and Dog(String name) |
Unit 1, Topic 1.13, p. 46 |
| Reference variable | Holds the address of an object, not the object itself: Dog d |
Unit 1, Topic 1.13, p. 46 |
null |
A reference that points to no object: Dog d = null; |
Unit 1, Topic 1.13, p. 46 |
| Alias | Two variables holding the same reference, so both name one object: Dog b = a; |
— |
newcreates the object and calls a constructor; the variable stores where the object is- Java always copies the variable when you assign it or pass it to a method. For an object, the copy is the address, so both copies reach the same object
Primitive vs. Reference Variables
Primitive (int, double, boolean) |
Reference (any object) | |
|---|---|---|
| What the variable holds | The value itself | The address of an object |
b = a; copies |
The value: two separate numbers | The address: two names for one object |
Changing through b |
Never affects a |
b.setName(...) changes the object a sees too |
| Passed to a method | The method gets a copy of the value | The method gets a copy of the address |
Where the Data Lives (Beyond the AP Exam)
| Stack | Heap | |
|---|---|---|
| Stores | Local variables: primitives and references | Every object |
| Freed | When the method returns | When no reference points to it any more |
| Picture it as | Your desk, with small items and notes | A playroom of toy boxes your notes point to |
Constructor Types
| Type | Behavior | Example |
|---|---|---|
| No-argument constructor | Sets default values | new Student() |
| Parameterized constructor | Sets values from its parameters | new Student("Soni", 11) |
Predicting What a Method Changes
- The method changes a primitive parameter → the caller’s variable does not change
- The method changes an object’s data through its parameter (
p.age++) → the caller sees the change, because it is the same object - The method assigns a new object to its parameter (
p = new Person(...)) → the caller’s variable does not change; only the method’s copy of the address moved - A variable is
null→ calling a method on it throws aNullPointerException
College Board Requirements
AP CSA Unit 1, Topic 1.13 Object Creation and Storage (Instantiation). Quoted from the course and exam description (College Board, 2025, p. 46):
- “A class contains constructors that are called to create objects. They have the same name as the class.”
- “An object is typically created using the keyword new followed by a call to one of the class’s constructors.”
The stack and heap are not part of the College Board framework. They come from the Java Virtual Machine specification, which defines where the JVM keeps method frames and objects (Lindholm et al., 2023, section 2.5).
Reference: College Board, AP Computer Science A Course and Exam Description (2025), Unit 1, Topic 1.13, pp. 46–47.
2. Real-Life Application
The Billion-Dollar Mistake
In 1965, computer scientist Tony Hoare added the null reference to a programming language he was designing, because it was easy to build. In a 2009 talk he called it his “billion-dollar mistake”: decades of crashes in many languages, Java included, came from code that used a reference that pointed to nothing. Everyone guess: what happens when your program calls a method on a variable that was never given an object?
| Real Case | What Happened | Java Idea in This Lesson |
|---|---|---|
| Null references (1965 to today) | Code used a reference that pointed to no object, and the program crashed | Dog d = null; d.bark(); throws a NullPointerException |
| A shared document link | Everyone with the link edits the same file | Dog b = a; makes an alias: two names, one object |
| A downloaded copy | Editing your copy does not change the original | new Dog(a.getName()) makes a separate object |
| A house address | The address on paper is not the house | A reference variable holds where the object is |
The takeaway: a variable of a class type holds a reference. Know whether two variables share one object or point to separate ones, and never call a method through null.
3. Tech Talk
Learning Objective: Create objects with new and constructors, explain what a reference variable stores, and predict what changes when two variables or a method share one object.
Success Criteria: You can call the right constructor, tell an alias from a copy, and predict whether a change made inside a method shows up back in main.
A class is the cookie cutter and each object is a cookie. new makes a cookie and the constructor decorates it:
Dog a = new Dog("Rex"); // a new object; a holds its address
Dog b = a; // no new object: b holds the same address
b.setName("Max");
System.out.println(a.getName()); // Max: a and b are aliases
Primitives behave differently, because the variable holds the value itself:
int x = 10;
int y = x; // a copy of the value
y = 20;
System.out.println(x); // 10
Code Examples
A. Constructors. The Student class has a no-argument constructor and a parameterized one
Java picks the constructor whose parameter list matches the arguments:
new Student() // matches Student()
new Student("Soni", 11) // matches Student(String name, int grade)
new Student("Soni") // no matching constructor: does not compile
Terminology:
Overloading means one class has several constructors with the same name and different parameter lists.
Code Runner Challenge
Predict both lines, run it, then add a third Student
View IPYNB Source
// CODE_RUNNER: Predict both lines, run it, then add a third Student
public class Student {
private String name;
private int grade;
// No-argument constructor: default values
public Student() {
name = "New Student";
grade = 9;
}
// Parameterized constructor: values from the caller
public Student(String name, int grade) {
this.name = name;
this.grade = grade;
}
public void display() {
System.out.println("Name: " + name + ", Grade: " + grade);
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student("Soni", 11);
s1.display();
s2.display();
}
}
Student.main(null);
B. Aliases and method calls. The ReferenceDemo class compares a primitive, an alias, and a method that changes an object
Watch three cases side by side:
int b = a; // copy of a value
Counter c2 = c1; // copy of an address: an alias
addTen(c1); // the method gets a copy of the address too
Terminology:
Alias: two reference variables holding the same address. A change made through either one is a change to the one shared object.
Code Runner Challenge
Predict all four lines, run it, then make c2 a separate Counter
View IPYNB Source
// CODE_RUNNER: Predict all four lines, run it, then make c2 a separate Counter
public class ReferenceDemo {
static class Counter {
int value;
Counter(int value) {
this.value = value;
}
}
public static void addTen(int n) {
n += 10; // changes only the method's copy
}
public static void addTen(Counter c) {
c.value += 10; // changes the shared object
}
public static void main(String[] args) {
int a = 5;
int b = a;
b = 99;
System.out.println("a = " + a);
Counter c1 = new Counter(5);
Counter c2 = c1; // alias, not a copy
c2.value = 99;
System.out.println("c1.value = " + c1.value);
addTen(a);
System.out.println("a after addTen = " + a);
addTen(c1);
System.out.println("c1.value after addTen = " + c1.value);
}
}
ReferenceDemo.main(null);
Hacks & Practice Tasks
Lesson: CSA 1.13 Object Creation and Storage (Instantiation)
MCQ 1.13: <paste the copied result line, such as 3/4 | answers: B,C,A,D>
Popcorn: twin is a separate object, John stays 21 (yes/no); predicted age after birthday = <p1>; predicted name after reassign = <p2>
Homework Hack 1: both constructors work, all three checks print true (yes/no)
Homework Hack 2: addBonus written, predicted score after tryReplace = <p>, both checks print true (yes/no)
Homework Hack 3: Pet constructors and adopted reference, all four checks print true (yes/no)
Submission Safety Rules (Read First)
- One class per cell, ending with
ClassName.main(null);. - Label the homework hacks 1 through 3.
- Write your predictions before you run each cell.
- Say “copy of the reference,” not “copy of the object,” when
=is used on objects. - Run each Java cell and leave the output showing.
- Include your MCQ score.
- Use
##headings or smaller.
Popcorn Hack (In-Class)
3-minute challenge: haveBirthday and reassignPerson look alike, but only one changes John.
- Before you run, predict John’s age after
haveBirthdayand his name afterreassignPersonin the Popcorn checker under the runner - Run it and compare
twinis supposed to be a separate copy of John, but right now it is an alias, so setting the twin’s age to 5 changes John too. Fix thetwinline withnew Person(...)so John stays 21- Run it again. When every check passes, copy the checker’s line into your submission notes
Code Runner Challenge
Predict in the checker, run it, then make twin a separate Person
View IPYNB Source
// CODE_RUNNER: Predict in the checker, run it, then make twin a separate Person
// Practice #1 - Try it yourself first!
public class PersonDemo {
static class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public static void haveBirthday(Person p) {
p.age += 1; // changes the object p points to
}
public static void reassignPerson(Person p) {
p = new Person("New Person", 99); // moves only the method's copy of the address
}
public static void main(String[] args) {
Person john = new Person("John", 20);
haveBirthday(john);
System.out.println("After birthday: " + john.name + " is " + john.age);
reassignPerson(john);
System.out.println("After reassign: " + john.name + " is " + john.age);
Person twin = john; // TODO: make twin a separate Person with John's name and age
twin.age = 5;
System.out.println("Twin is " + twin.age + ", John is " + john.age);
}
}
PersonDemo.main(null);
Before you press Run, predict this output.
4. MCQ Check
4 questions, one at a time. Answer, check, then go to the next one. At the end, copy the score line into your submission notes.
5. Homework Hack
Task: Do all three hacks. Each starter compiles and runs, so you can run the initial version and compare its incorrect output with the expected output. Change only what the TODO asks for, and keep going until every check prints true.
Solution Skeleton:
Hack 1: Two Constructors (5 minutes)
Constructors. Book() should make a book titled "Untitled" with 0 pages. Book(String title, int pages) should use its parameters. Fill in both constructor bodies.
Code Runner Challenge
Fill in both constructors so all three checks print true.
View IPYNB Source
// CODE_RUNNER: Fill in both constructors so all three checks print true.
public class ConstructorHack {
static class Book {
private String title;
private int pages;
public Book() {
// TODO: title is "Untitled", pages is 0
}
public Book(String title, int pages) {
// TODO: store both parameters
}
public String getTitle() { return title; }
public int getPages() { return pages; }
}
public static void main(String[] args) {
Book blank = new Book();
Book novel = new Book("Holes", 233);
System.out.println(blank.getTitle() + ", " + novel.getTitle());
System.out.println("Untitled".equals(blank.getTitle()));
System.out.println("Holes".equals(novel.getTitle()));
System.out.println(novel.getPages() == 233);
}
}
ConstructorHack.main(null);
Hack 2: References in Methods (5 minutes)
Passing a reference. Write addBonus so it raises the player’s score through the reference it is given. Then predict, before you run, what tryReplace leaves in hero.score, and write it in predictedScore.
Code Runner Challenge
Write addBonus, set predictedScore before you run, and run until both checks print true.
View IPYNB Source
// CODE_RUNNER: Write addBonus, set predictedScore before you run, and run until both checks print true.
public class ReferenceHack {
static class Player {
int score;
Player(int score) {
this.score = score;
}
}
// TODO: add bonus to the player's score
public static void addBonus(Player p, int bonus) {
}
public static void tryReplace(Player p) {
p = new Player(0);
p.score += 1000;
}
public static void main(String[] args) {
Player hero = new Player(100);
addBonus(hero, 50);
System.out.println("After addBonus: " + hero.score);
int predictedScore = -1; // TODO: what is hero.score after tryReplace? Write it BEFORE you run
tryReplace(hero);
System.out.println("After tryReplace: " + hero.score);
System.out.println(hero.score == 150);
System.out.println(predictedScore == hero.score);
}
}
ReferenceHack.main(null);
Hack 3: Adoption Day (10 minutes)
Combined. Finish both Pet constructors: Pet(String name) starts at age 0, and Pet(String name, int age) uses both values. Then point adopted at the same object as luna, without creating another Pet. The last check uses ==, which is true only when two references point to the same object.
Code Runner Challenge
Finish both constructors and point adopted at luna so all four checks print true.
View IPYNB Source
// CODE_RUNNER: Finish both constructors and point adopted at luna so all four checks print true.
public class AdoptionHack {
static class Pet {
private String name;
private int age;
public Pet(String name) {
// TODO: store name; age starts at 0
}
public Pet(String name, int age) {
// TODO: store both
}
public String getName() { return name; }
public int getAge() { return age; }
}
public static void main(String[] args) {
Pet milo = new Pet("Milo");
Pet luna = new Pet("Luna", 3);
Pet adopted = null; // TODO: point adopted at luna (do not use new)
System.out.println(adopted == null ? "Nobody adopted yet" : "Adopted " + adopted.getName());
System.out.println(milo.getAge() == 0 && "Milo".equals(milo.getName()));
System.out.println(luna.getAge() == 3);
System.out.println(adopted != null && "Luna".equals(adopted.getName()));
System.out.println(adopted == luna);
}
}
AdoptionHack.main(null);
Grading Plan (1 Point Total)
| Part | Points | What earns the points |
|---|---|---|
| Popcorn | 0.2 | Both predictions written before running, and twin is a separate Person made with new, so John stays 21. The Popcorn checker line is in the notes. |
| MCQ | 0.2 | 4 correct. 0.15 for 2 or 3, 0.1 if every question was answered. |
| Homework Hack 1 | 0.15 | Both constructors set every field, and all three checks print true. |
| Homework Hack 2 | 0.25 | addBonus changes the object through its parameter, predictedScore was written before running, and both checks print true. |
| Homework Hack 3 | 0.2 | Both Pet constructors work, adopted refers to luna without a new object, and all four checks print true. |
| Total | 1.0 |
Quick Validation Checklist
- Each cell ends with
ClassName.main(null);and shows output. - Popcorn checker line in the notes.
- MCQ score in the notes.
- Predictions written before running.
- Object assignment described as copying the reference, not the object.
- Every homework check prints
true.
6. LxD Cycle Process
Empathize: Students think = always makes an independent copy, the way it does for int. When two object variables end up naming the same object and a change through one shows up in the other, they assume something broke, instead of seeing the defined behavior of references.
Define:
- POV: CSA students need a picture of what a reference variable holds, because without it, the behavior of primitives and objects looks like two inconsistent rules instead of one rule (always copy the variable) applied to two kinds of values.
- Learning Goal: Students will create objects with constructors, explain what a reference variable stores, and predict whether a change made through an alias or inside a method affects the caller.
Ideate:
- HMW Question: How might we make something invisible, the address in a variable, visible enough that students can predict output?
- HMW Question: How might we separate “change the object” from “point the variable somewhere else,” which look alike in code?
- Activity: A desk-and-playroom analogy for where values live, then predict-then-run hacks on aliases, constructors, and references passed to methods.
Prototype:
- A reference guide with a primitive-versus-reference table, runnable constructor and alias examples, a prediction Popcorn Hack with a checker, an MCQ knowledge check, three scaffolded homework hacks with self-checking output, and a grading rubric.
- A trial run showed that students could recite the rule but still predicted the wrong output for
reassignPerson, so the Reference Guide now contrasts changing an object with reassigning a parameter, and the Popcorn Hack asks for both predictions. - The old version called object parameters “pass-by-reference.” Java always passes a copy of the variable; for an object, that copy is the reference. The lesson now uses that wording.
Test:
- Ask peers in peer review to complete the practice without additional explanation.
- Observe whether peers predict the
reassignPersonandtryReplaceresults correctly, and whether they say “copy of the reference.” - Compare your lesson with another that is posted.
- Use the findings to revise any instruction or rubric criterion that did not guide students clearly.
- On submission, collect evidence from runner output, MCQ results, AI grading and student explanations.
- After teaching, grading and analysis, come back and revise lesson to complete teaching cycle for continuous improvement.
7. References/Citations
College Board. (2025). AP Computer Science A course and exam description [Effective fall 2025]. https://apcentral.collegeboard.org/media/pdf/ap-computer-science-a-course-and-exam-description-effective-fall-2025.pdf
Hoare, T. (2009, March). Null references: The billion dollar mistake [Conference presentation]. QCon London, London, United Kingdom.
Lindholm, T., Yellin, F., Bracha, G., Buckley, A., & Smith, D. (2023). The Java® virtual machine specification (Java SE 21 ed., section 2.5, Run-time data areas). Oracle. https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-2.html
Submit Assignment
Need to update a submission later? Open the submissions dashboard.