1.12 Classes & Objects
Understand that objects are instances of classes with data and methods.
1. Reference Guide
Key Topics
| Term | Definition | Example |
|---|---|---|
| Class | A blueprint / template for objects. Defines what data and methods every object of that type will have. | class Dog { ... } |
| Object | One instance created from a class, with its own copy of the fields. | new Dog() |
new |
The keyword that creates an object and gives you a reference to it. | Dog myDog = new Dog(); |
| Reference variable | A variable that points to an object. It does not contain a separate copy of the object. | Dog myDog |
| Field / instance variable | A variable defined in a class; each object gets its own copy. | String name; |
| Aliasing | Two or more reference variables pointing to the same object. | Dog d2 = d1; |
Object |
The superclass of every Java class. Provides methods like toString(), equals(Object), getClass(). |
x instanceof Object |
Class vs. Object vs. Reference Variable
| Piece | What it is | Example |
|---|---|---|
| Class | The blueprint | class Dog { ... } |
| Object | One thing built from the blueprint, living in memory | the result of new Dog() |
| Reference variable | A name that points at an object | Dog myDog |
Reading Assignment Statements
| Statement | What actually happens |
|---|---|
Dog d1 = new Dog(); |
Creates a new Dog object; d1 points to it. |
Dog d2 = d1; |
Copies the reference, not the object. d1 and d2 now point to the same Dog. |
d2.name = "Kairo"; |
Changes the one shared object, so d1.name also sees "Kairo". |
Dog d3 = new Dog(); |
Creates a second, separate Dog object, unrelated to d1 and d2. |
2. LxD Cycle Process
Empathize: Students often treat a class, an object, and a reference variable as three names for the same thing. The blueprint analogy can also suggest that assigning d2 = d1 builds a second object, so students are surprised when a change through d2 is visible through d1. The capitalization difference between an object in general and Java’s Object class can create another misconception.
Define:
- POV: Students need a model that separates the class definition, the runtime object, and each reference variable, because code syntax alone does not show aliasing.
- Learning Issue: Students need to trace reference assignment as copying a reference value, not copying the object’s fields.
- Learning Goal: Students will identify a class, an object, and a reference variable in a code example, predict the effect of aliasing two reference variables, and name a method every object inherits from
Object.
Ideate:
- HMW Question: How might we draw references so two variables pointing to one object cannot be mistaken for two objects?
- HMW Question: How might we distinguish the general word “object” from the Java class
Objectwhile keeping their relationship clear? - Activity: Draw a box-and-arrow memory model for
Dog d1 = new Dog(); Dog d2 = d1;, predict the result of changingd2.name, and then verify the prediction in code.
Prototype:
- A reference guide, runnable Java examples, a diagram-and-predict popcorn hack, an MCQ knowledge check, and a scaffolded
Bookclass rubric. - Students revise their diagram after seeing runner output.
- Excellence means explaining why two variables share one object, not only predicting the printed value.
Test:
- Ask a partner to update the diagram after each statement without running the program.
- Compare the final prediction with the output.
- Revise any symbol that encouraged the partner to invent an extra object.
- On submission, collect evidence from runner output, MCQ results, AI grading, and student explanations.
- After teaching, grading, and analysis, come back and revise the lesson to complete the teaching cycle for continuous improvement.
3. College Board Requirements
AP CSA Unit 1, Topic 1.12 Classes & Objects. From the course and exam description (College Board, 2025, p. 45): an object is a “specific instance of a class.”
This lesson covers these Topic 1.12 ideas (paraphrased; check exact wording and numbering in the CED):
- A class is a template that specifies the data (fields) and behavior (methods) its objects will have.
- An object is a specific instance of a class, created with the
newkeyword. - A reference variable holds an object reference (the object’s location), not the object itself. The Java Language Specification distinguishes an object from the reference values that point to it (Gosling et al., 2025, §§4.3.1-4.3.2).
- Assigning one reference variable to another copies the reference, so both variables refer to the same object; a change made through either variable is visible through both.
Objectis the superclass of every class in Java (Gosling et al., 2025, §4.3.2), so every object has access to methods such astoString(),equals(Object), andgetClass().
4. Lesson Plan
Learning Objective: Understand the difference between classes and objects, how reference variables work, and why every Java class inherits from Object.
Success Criteria: Given a class and object example, you can identify the class, the object, and the reference variable.
Tech Talk (5 minutes)
A class is a blueprint. An object is a specific instance made from that blueprint.
Code Runner Challenge
Run it, then change myDog's name and age and predict the new output
View IPYNB Source
class Dog {
String name;
int age;
void bark() {
System.out.println(name + " says woof!");
}
}
Dog myDog = new Dog();
myDog.name = "Alo";
myDog.age = 3;
myDog.bark();
Dog is the class. myDog is a reference variable pointing to a Dog object.
Code Runner Challenge
Run it, then change myDog.name and myDog.age and predict the new output.
Code Runner Challenge
Run it, then add a Dog d3 = new Dog(); and show it is a separate object
View IPYNB Source
// CODE_RUNNER: Run it, then change myDog's name and age and predict the new output
class Dog {
String name;
int age;
void bark() {
System.out.println(name + " says woof!");
}
}
public class ClassObjectDemo {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.name = "Alo";
myDog.age = 3;
myDog.bark();
System.out.println(myDog.name + " is " + myDog.age + " years old.");
}
}
ClassObjectDemo.main(null);
5. Code Examples
A. Reference variables and aliasing
The AliasDemo class shows that d2 = d1 does not create a second Dog. It copies the reference, so both variables point to the same object.
Code Runner Challenge
Run it, then predict what happens if you change e1.name after this
View IPYNB Source
Dog d1 = new Dog();
d1.name = "Kai";
Dog d2 = d1;
d2.name = "Kairo";
System.out.println(d1.name); // Kairo
Terminology: d1 and d2 are aliases: two names for one object. Changing the object through either name is visible through both.
Code Runner Challenge
Run it, then try printing x directly with System.out.println(x)
View IPYNB Source
// CODE_RUNNER: Run it, then add a Dog d3 = new Dog(); and show it is a separate object
class Dog {
String name;
}
public class AliasDemo {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.name = "Kai";
Dog d2 = d1; // copies the reference, not the object
d2.name = "Kairo"; // changes the one shared object
System.out.println("d1.name: " + d1.name); // Kairo
System.out.println("d2.name: " + d2.name); // Kairo
System.out.println("Same object? " + (d1 == d2)); // true
}
}
AliasDemo.main(null);
B. Two separate objects
The SeparateObjects class shows the opposite case: two different new Dog() calls make two independent objects, so changing one never affects the other.
Terminology: == on reference variables checks whether they point to the same object, not whether their fields hold equal values.
Code Runner Challenge
Finish printInfo(), create one Book, set its fields, and call it
View IPYNB Source
// CODE_RUNNER: Run it, then predict what happens if you change e1.name after this
class Dog {
String name;
}
public class SeparateObjects {
public static void main(String[] args) {
Dog e1 = new Dog();
e1.name = "Bo";
Dog e2 = new Dog(); // a second, separate object
e2.name = "Bo";
System.out.println("e1.name: " + e1.name);
System.out.println("e2.name: " + e2.name);
System.out.println("Same object? " + (e1 == e2)); // false, even though names match
}
}
SeparateObjects.main(null);
C. Every class inherits from Object
Every Java class ultimately extends Object. That means every object has access to methods such as toString(), equals(Object), and getClass(), even when the class never mentions Object.
Code Runner Challenge
Write your predictions, finish the class and main, then run it
View IPYNB Source
Dog x = new Dog();
System.out.println(x.getClass().getName());
System.out.println(x instanceof Object); // true
Terminology: You do not need to write class Dog extends Object. Java adds it automatically.
// CODE_RUNNER: Run it, then try printing x directly with System.out.println(x)
class Dog {
String name;
}
public class ObjectDemo {
public static void main(String[] args) {
Dog x = new Dog();
x.name = "Rex";
System.out.println(x.getClass().getName()); // Dog
System.out.println(x instanceof Object); // true
// Every object inherits toString(), even without overriding it
System.out.println(x.toString()); // Dog@<hash>, unless Dog overrides toString
}
}
ObjectDemo.main(null);
6. Hacks & Practice Tasks
Prepare your submission IPYNB
- Create a new notebook in your portfolio homework area:
_notebooks/homework. - Add one raw cell at the top with the frontmatter.
- Add code cells for the Popcorn Hack and the Homework Hack. Make sure every cell runs with visible output.
- Submit the link to your published page at the bottom of this page, and paste this in the description box:
Lesson: CSA 1.12 Classes & Objects
MCQ 1.12: <paste your score line, such as 4/5 | answers: B,C,A,D,A>
Popcorn: Book class finished, one Book object created and printInfo() called
Homework: two Author objects created, aliasing predicted before running (yes/no)
Homework: one Object-inherited method named and demonstrated
Submission Safety Rules (Read First)
- One class per cell, ending with
ClassName.main(null);. - Run each cell and leave the output showing.
- Write your own answers, not the sample code from the lesson.
- Include your MCQ score.
- Use
##headings or smaller.
Popcorn Hack (In-Class)
2-minute challenge:
- Finish
Bookby addingprintInfo(). - Create one
Bookobject, set its fields, and callprintInfo().
// CODE_RUNNER: Finish printInfo(), create one Book, set its fields, and call it
class Book {
String title;
int pages;
// TODO: write printInfo() to print the title and page count
}
public class MainPopcorn {
public static void main(String[] args) {
// TODO: make one Book, set its fields, and call printInfo()
}
}
MainPopcorn.main(null);
MCQ Check
5 questions, one at a time. Answer, check, then go to the next one. At the end, copy your score line (for example 4/5 | answers: B,C,A,D,A) into your submission notes.
Question 1 of 5
What best describes the relationship between a class and an object?
- A. A class is one specific object.
- B. A class is a blueprint; an object is an instance made from it.
- C. A class and an object are the same thing.
- D. An object contains many classes.
Check answer
**B.** A class defines what data and methods objects of that type will have. An object is one instance built from that class with `new`.Question 2 of 5
Dog d1 = new Dog();
Dog d2 = d1;
d2.name = "Kairo";
What does d1.name print after this?
- A.
null, becaused1was never changed - B.
"Kairo", becaused1andd2point to the same object - C. A compile error
- D. The original value of
d1.namebefored2was created
Check answer
**B.** `Dog d2 = d1;` copies the reference, not the object, so `d1` and `d2` point to the same `Dog`. Changing the object through `d2` is visible through `d1`.Question 3 of 5
Dog e1 = new Dog();
Dog e2 = new Dog();
Which is true?
- A.
e1ande2refer to the same object. - B.
e1ande2refer to two separate objects. - C.
e2is a copy ofe1’s fields. - D. This code does not compile.
Check answer
**B.** Each `new Dog()` call creates a distinct object. `e1` and `e2` are unrelated, even if their fields later hold equal values.Question 4 of 5
Which method is available on every Java object, even if its class never mentions it?
- A.
main() - B.
toString() - C.
printInfo() - D.
bark()
Check answer
**B.** Every class inherits from `Object`, so every object has `toString()`, `equals(Object)`, and `getClass()`, among others.Question 5 of 5
Dog a = new Dog(); Dog b = new Dog(); Both a.name and b.name are set to "Rex". What does a == b evaluate to?
- A.
true, because the names match - B.
false, becauseaandbpoint to two different objects - C. A compile error
- D. It depends on which was created first
Check answer
**B.** `==` on reference variables compares whether they point to the *same* object, not whether their field values are equal. `a` and `b` are separate objects.Homework Hack
Task: Create an Author class with a name field and a method that prints it. In main, create two Author reference variables where one is aliased to the other (like d1/d2 above), and two more that point to separate objects (like e1/e2). Before running, write your prediction for each name printout, then check it against the real output. Finally, call one method inherited from Object (such as getClass()) on any Author object and print the result.
// CODE_RUNNER: Write your predictions, finish the class and main, then run it
class Author {
String name;
// TODO: write a method that prints the author's name
}
public class HomeworkClassesObjects {
// Predict BEFORE running
static String predictAliased = "?"; // what will the aliased pair print?
static String predictSeparate = "?"; // what will the separate pair print?
public static void main(String[] args) {
System.out.println("Predictions: aliased=" + predictAliased + ", separate=" + predictSeparate);
// TODO: create Author a1 = new Author(); and Author a2 = a1; (aliased pair)
// set a name through a2, then print both a1's and a2's name
// TODO: create Author b1 = new Author(); and Author b2 = new Author(); (separate pair)
// set the same name on both, then print b1 == b2
// TODO: call one Object-inherited method (like getClass()) on any Author and print it
}
}
HomeworkClassesObjects.main(null);
Grading Plan (1 Point Total)
| Part | Points | What earns the points |
|---|---|---|
| Popcorn | 0.2 | Book finished, one object created, printInfo() called, and the cell runs. |
| MCQ | 0.2 | 4 or 5 correct. 0.15 for 3, 0.1 if every question was answered. |
| Homework: class/object usage | 0.2 | Author class and objects created correctly with new. |
| Homework: aliasing prediction | 0.25 | Prediction written before running, and the aliased pair’s shared value is correctly demonstrated. |
| Homework: separate objects | 0.1 | Separate pair correctly shown as two different objects (== is false). |
Homework: Object method |
0.05 | One method inherited from Object is called and its result printed. |
| Total | 1.0 |
Quick Validation Checklist
- Each cell ends with
ClassName.main(null);and shows output. - MCQ score in the notes.
Bookpopcorn hack has one object with fields set andprintInfo()called.- Homework has both an aliased pair and a separate pair of
Authorobjects. - Predictions were written before running, not after.
- One
Object-inherited method is named and demonstrated.
7. Lesson Revisions
Revision Made: Added more MCQ questions and organized structure with lesson plans made more clear.
8. Feedback Evidence
Feedback Received: Added sections made simple lesson a bit lengthy so reference section moved to the top for those looking for quick review.
9. References
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
Gosling, J., Joy, B., Steele, G., Bracha, G., Buckley, A., Smith, D., & Bierman, G. (2025). The Java language specification: Java SE 25 edition (Secs. 4.3.1-4.3.2). Oracle. https://docs.oracle.com/javase/specs/jls/se25/html/jls-4.html#jls-4.3.1
Submit Assignment
Need to update a submission later? Open the submissions dashboard.