1.13 Object Creation and Storage (Instantiation)
Create objects with new and constructors, storing references in variables.
1. Reference Guide
Key Topics
| Term | Definition | Example |
|---|---|---|
| Instantiation | Creating an actual object from a class blueprint using new. |
Dog myDog = new Dog(); |
| Constructor | A special method, matching the class name, that initializes a new object. | public Dog() { ... } |
| Stack | Memory that stores primitives and object references; freed automatically when a method returns. | int age = 16; |
| Heap | Memory that stores every object and array; a stack variable only holds its address. | new Dog() |
| Pass-by-value | A method receives a copy of a primitive’s value. | changing the copy leaves the original untouched |
| Pass-by-reference | A method receives a copy of an object’s address. | changing the object’s contents changes the original |
- Primitives (
int,double,boolean) live entirely on the stack - Objects live on the heap; only their address lives on the stack
Stack vs. Heap
| Stack | Heap | |
|---|---|---|
| Stores | Primitives and object references | Objects and arrays |
| Scope | Private per thread | Shared among all threads |
| Managed | Automatically freed when a method returns | Freed once no reference points to it |
| Speed | Fast allocation | Slower allocation |
| Failure mode | StackOverflowError if too deep |
Larger, but not infinite |
Pass-by-Value vs. Pass-by-Reference
| Pass-by-Value (primitives) | Pass-by-Reference (objects) | |
|---|---|---|
| What’s copied | The value itself | The reference (address) |
| Modify contents in method | Original is unaffected | Original object’s contents ARE changed |
| Reassign parameter in method | Has no meaning beyond the copy | Original reference is still unaffected — only the local copy of the address changed |
- Primitives contain their actual value
- A reference variable contains the memory address of the object on the heap
- Method parameters are always initialized with a copy — of a value for primitives, of an address for objects
Constructor Types
| Type | Behavior |
|---|---|
| Default constructor | No parameters; sets default values |
| Parameterized constructor | Takes parameters to set specific initial values |
| Copy constructor | Creates a new object as a copy of an existing object |
Spotting Stack vs. Heap in Code
- Parameter changes inside a method, caller’s variable doesn’t → the parameter was a primitive (pass-by-value)
- Parameter’s contents change inside a method, and the caller’s variable changes too → the parameter was a reference, and its contents were modified
- A method reassigns the parameter to a brand-new object → the caller’s variable is unaffected, because only the local copy of the address changed
2. LxD Cycle Process
Empathize: I noticed students think of = as always making an independent copy, the way it does for int. When two object variables end up pointing at the same object, and changing one appears to change the other, students assume something went wrong, instead of recognizing this as the defined behavior of reference types.
Define:
- POV: CSA students need a mental model of where data actually lives (stack vs. heap) because without it, pass-by-value and pass-by-reference look like inconsistent, unpredictable rules instead of one consistent rule applied to two different kinds of data.
- Learning Goal: Students will distinguish stack memory from heap memory, explain why primitives are pass-by-value while objects are pass-by-reference, and create objects correctly using constructors.
Ideate:
- HMW Question: How might we make an invisible thing (memory) visible enough that students can predict what a program will print before running it?
- Activity: A toy analogy — your desk (stack) holds small toys directly, while a playroom (heap) holds big toy boxes that your desk only holds the address to — paired with “predict the output, then run it” popcorn hacks.
Prototype & Test: In a trial run, students could recite “objects are pass-by-reference” but still predicted the wrong output for the reassignPerson popcorn hack, because reassigning a reference inside a method looked identical to them as modifying the object through a reference. I added an explicit contrast between those two operations in the Reference Guide.
3. 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/heap distinction taught in this lesson through the desk-and-playroom analogy is not itself part of the College Board framework — it comes from the Java Virtual Machine Specification (Lindholm et al., 2023), which formally defines where the JVM stores frames and objects (see References).
4. Lesson Plan
Learning Objective: By the end of this lesson, you will be able to explain how Java assigns memory for objects and primitives, tell the difference between stack and heap memory, explain the difference between pass-by-value and pass-by-reference, and create and instantiate objects using constructors.
Success Criteria: You can predict, before running the code, whether changing a variable inside a method will affect the original variable back in main, based on whether that variable holds a primitive or a reference.
Tech Talk & Introduction (5 minutes)
Instantiation is the process of creating an actual object from a class blueprint — think of a class as a cookie cutter and objects as the cookies it makes. new Dog() allocates memory and initializes it based on the Dog blueprint.
In Java, memory for variables lives in two places:
- Stack — your desk. Small, primitive values (and object references) live here, one per method call, freed automatically when the method returns.
- Heap — the playroom in another room. Every object and array actually lives here; a stack variable only holds the address of where to find it.
Why do we do this? A primitive variable holds its value directly, so passing it to a method passes a copy — the original is untouched. A reference variable holds an address, so passing it to a method passes a copy of the address — the method can still reach into the heap and change the object’s contents, but reassigning the local reference itself never affects the caller’s variable.
5. Code Examples
A. Simple: Instantiation
// Class Dog is the blueprint
class Dog {
String name;
int age;
}
// Instantiation creates objects from the blueprint
Dog myDog = new Dog(); // Creating an instance
Dog yourDog = new Dog(); // Creating another instance
Think of a class as a cookie cutter and objects as the actual cookies you make with it!

B. Simple: Stack Memory
Stack memory behaves like a stack of plates — Last-In-First-Out. Primitives (int, double, boolean) live here directly.
public class StackDemo {
public static void main(String[] args) {
int number = 100; // Primitive stored in stack
double price = 49.99; // Primitive stored in stack
boolean isActive = true; // Primitive stored in stack
String name; // Reference stored in stack (and in heap)
System.out.println("Number: " + number);
System.out.println("Price: " + price);
System.out.println("Is Active: " + isActive);
}
}
StackDemo.main(null);
C. Intermediate: Heap Memory
The heap is like a playroom full of toy boxes: new String(...), new int[5], and new Dog() all create objects on the heap, while only their addresses live on the stack.
public class HeapDemo {
public static void main(String[] args) {
// Explicit object creation
String message = new String("Hello"); // Object created on heap,
// message is referenced in the stack
// Arrays stored on heap
int[] numbers = new int[5]; // Array object on heap
// numbers reference is on stack
// Custom objects
Dog myDog = new Dog(); // Dog object on heap
// myDog reference is on stack
System.out.println("Message: " + message);
System.out.println("Numbers array length: " + numbers.length);
}
}
HeapDemo.main(null);
D. Intermediate: Pass-by-Value
Imagine handing a friend a copy of your toy car — if they paint the copy red, your original toy doesn’t change. That’s what happens when a primitive is passed to a method.
public class IntByValue {
// Method tries to change the number
public static void changeInt(int n) {
n = n + 10; // only changes the copy
System.out.println("Inside method: n = " + n);
}
public static void main(String[] args) {
int n = 5; // original number
System.out.println("Before method: n = " + n);
changeInt(n); // pass copy of n
System.out.println("After method: n = " + n); // still 5
}
}
// Run main manually
IntByValue.main(null);
Before method: n = 5
Inside method: n = 15
After method: n = 5
E. Complex: Pass-by-Reference
Now imagine a shared toy box: you hand a friend the box’s address, not a copy. If they change a toy inside it, your box changed too, because it is the same box.
// A simple class to hold a number
class NumberHolder {
int value;
NumberHolder(int value) {
this.value = value;
}
}
public class PassByReferenceDemo {
// Method that changes the object's value
public static void changeValue(NumberHolder n) {
n.value = n.value + 10; // modify the object
}
public static void main(String[] args) {
NumberHolder myNumber = new NumberHolder(5); // create object
System.out.println("Before: " + myNumber.value); // 5
changeValue(myNumber); // pass object reference
System.out.println("After: " + myNumber.value); // 15
}
}
PassByReferenceDemo.main(null);
Before: 5
After: 15
F. Complex: Constructors and Object Creation
Constructors are special methods, matching the class name, that initialize an object’s attributes at the moment it is created with new.
// Basics:
public class Student {
private String name;
private int grade;
private double gpa;
// Default constructor
public Student() {
this.name = "Nora";
this.grade = 111;
this.gpa = 5.0;
}
// Parameterized constructor
public Student(String name, int grade, double gpa) {
this.name = name;
this.grade = grade;
this.gpa = gpa;
}
public void display() {
System.out.println("Name: " + name + ", Grade: " + grade + ", GPA: " + gpa);
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student("Soni", 11, 1.0);
s1.display();
s2.display();
}
}
Student.main(null);
Name: Nora, Grade: 111, GPA: 5.0
Name: Soni, Grade: 11, GPA: 1.0
6. Hacks & Practice Tasks
Submission Safety Rules (Read First)
[!IMPORTANT] To avoid grading errors, follow these rules exactly:
- Write your prediction before you run each Popcorn Hack cell.
- Run every code cell and leave the output visible.
- Answer every task/question in full sentences, referencing stack vs. heap by name.
Popcorn Hack #1: Stack vs Heap
[!TIP] Before running, predict: will changing
baffecta? Will changingarray2affectarray1? Write both predictions down first.
Run the following code and observe:
public class MemoryDemo {
public static void main(String[] args) {
// Stack variables
int a = 10;
int b = a; // Copy of value
b = 20; // Changing b doesn't affect a
System.out.println("Primitives (Stack):");
System.out.println("a = " + a); // Still 10
System.out.println("b = " + b); // Now it's 20
// Heap variables
int[] array1 = {1, 2, 3};
int[] array2 = array1; // Copy of reference (address)
array2[0] = 99; // Changing array2 DOES affect array1
System.out.println("\nArrays (Heap):");
System.out.println("array1[0] = " + array1[0]); // Now it's 99!
System.out.println("array2[0] = " + array2[0]); // Also 99
}
}
MemoryDemo.main(null);
Primitives (Stack):
a = 10
b = 20
Arrays (Heap):
array1[0] = 99
array2[0] = 99
Tasks (answer in a few sentences):
- Why does changing
bnot affecta, but changingarray2affectsarray1? - Describe what’s on the stack vs. the heap for this code.
Popcorn Hack #2: Understanding Pass-by-Reference
[!TIP]
haveBirthdayandreassignPersonlook similar. One modifies the object through the reference; the other replaces the local reference with a brand-new object. Only one of them changes whatjohnrefers to back inmain.
Examine the code below and predict the output before running it:
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 = p.age + 1; // Modifying object content
System.out.println("Inside method: " + p.name + " is now " + p.age);
}
public static void reassignPerson(Person p) {
p = new Person("New Person", 99); // Reassigning reference
System.out.println("Inside reassign: " + p.name + " is " + p.age);
}
public static void main(String[] args) {
Person john = new Person("John", 20);
System.out.println("Before birthday: " + john.name + " is " + john.age);
haveBirthday(john);
System.out.println("After birthday: " + john.name + " is " + john.age);
System.out.println("\nBefore reassign: " + john.name + " is " + john.age);
reassignPerson(john);
System.out.println("After reassign: " + john.name + " is " + john.age);
}
}
PersonDemo.main(null);
Before birthday: John is 20
Inside method: John is now 21
After birthday: John is 21
Before reassign: John is 21
Inside reassign: New Person is 99
After reassign: John is 21
Questions:
- After
haveBirthday(john)is called, what is John’s age? Why? - After
reassignPerson(john)is called, what is John’s name and age? Why? - Explain the difference between modifying an object’s contents vs. reassigning a reference.
Homework Hack
Once you’ve finished the lesson and run/answered both Popcorn Hacks above, continue to the full homework assignment:
Grading Plan (1 Point Total)
Classroom Rubric
-
0.2 points: Popcorn completion Both Popcorn Hacks include a written prediction made before running the cell, and all tasks/questions are answered in full sentences.
-
0.8 points: Homework completion (see the linked homework page for the full rubric)
- Correct use of stack/heap vocabulary when describing where data lives
- Correct prediction and explanation of pass-by-value vs. pass-by-reference scenarios
- At least one object correctly created and initialized using a constructor
Quick Validation Checklist
- Present: a prediction written before each Popcorn Hack was run
- Present: correct identification of what’s on the stack vs. the heap
- Present: at least one constructor call (
new ClassName(...)) - Absent: describing object assignment as “copying the object” instead of “copying the reference”
7. Lesson Revisions
Revision Made: I fixed both image tags to (the original had a typo,, missing the final “l,” which silently broke the image link) and replaced the outdated, unverified page citation with a directly quoted Essential Knowledge statement and a verified page number from the current CED. I also consolidated the “College Board tests these concepts” bullet lists that used to be interleaved in prose between code cells into the Stack vs. Heap and Pass-by-Value vs. Pass-by-Reference comparison tables in the Reference Guide, so students see both concepts side by side instead of as a list to memorize.
8. Feedback Evidence
Feedback Received: Peer review found two problems: the image tags used ``, a typo missing the final “l” that silently breaks the image link, and the old Resources section cited “PAGE 53-54” of the College Board CED for this topic, which does not match the 2025 edition (Topic 1.13 is actually on pp. 46-47).
9. References
College Board. (2025). AP Computer Science A: Course and exam description. https://apcentral.collegeboard.org/media/pdf/ap-computer-science-a-course-and-exam-description.pdf
Lindholm, T., Yellin, F., Bracha, G., Buckley, A., & Smith, D. (2023). The Java Virtual Machine specification: Java SE 21 edition (§2.5, Run-Time Data Areas). Oracle America. https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-2.html
Submit Assignment
Need to update a submission later? Open the submissions dashboard.