Lesson 1.12: Objects: Instances of Classes | AP CSA

Unit 1 · Lesson 1.12 · Conceptual

Lesson 1.12: Objects: Instances of Classes

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

What you'll learn in this lesson

  • 1.12.A. Explain the relationship between a class and an object — the class is the blueprint; an object is a specific instance with defined attribute values.
  • 1.12.A. Describe the inheritance relationship every class has with Object, and know the AP exclusion on designing inheritance hierarchies.
  • 1.12.B. Declare variables of reference types and explain what a reference variable holds.

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

AP exam weight: The class/object distinction underpins every OOP question. A reference variable holding a memory address — not the object itself — is essential for understanding NullPointerException and aliasing questions.

Key Vocabulary

Term Definition
class The formal definition (blueprint) specifying the attributes and behaviors all instances of that type will have.
object A specific instance of a class with its own set of attribute values, created at run time.
reference variable A variable that holds the memory address of an object, not the object's data directly.
null The value a reference variable holds when no object has been assigned to it.
aliasing When two or more reference variables hold the same object address, pointing to the same object in memory.
Object class The superclass of every Java class; provides toString() and equals() to all objects.
NullPointerException Thrown at run time when an instance method is called on a null reference.

Class vs Object: blueprint vs instance

A class is the formal definition — the blueprint specifying attributes and behaviors every instance will have. An object is a specific instance created at run time with its own attribute values.

The Core Distinction

String is a class. "hello" and "world" are two distinct String objects, each with its own character sequence. You can create as many objects as you need from one class definition.

Reference variables: holding an address

A variable of a reference type holds a memory address pointing to the object. It does not hold the object’s data directly.

The Rule

String name;           // holds null -- no object yet
name = "Tanner";       // now holds the address of the String object

Two reference variables can hold the same address (aliasing). Reassigning one variable does not affect the other.

AP Trap: == on reference types

== on reference types compares addresses, not content. Two separately created objects with identical data will have different addresses, so == returns false. Use .equals() to compare String content.

Every class extends Object

All Java classes are subclasses of Object (java.lang), giving every object toString() and equals(). Designing inheritance hierarchies yourself is outside AP exam scope.

▶ 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 of the following best describes the relationship between a class and an object?
A. An object is the definition; a class is a specific instance created at run time.
B. A class and an object are interchangeable terms.
C. A class is the blueprint defining attributes and behaviors; an object is a specific instance with its own attribute values.
D. A class is only needed when multiple objects of the same type are required.
C. Class = blueprint/template. Object = specific instance. A has it backwards. B is wrong. D is wrong: a single object still requires a class.
A student declares String message; without assigning a value. Which is true?
A. message holds null because no object has been assigned.
B. message holds "" by default.
C. A compile error occurs because String variables must be initialized at declaration.
D. message holds memory address 0.
A. Unassigned reference variables hold null. Java does not default to empty String. D is wrong: null is not a literal address value.
Which statement about Java’s Object class is NOT true?
A. All Java classes are subclasses of Object.
B. Every object has a toString() method inherited from Object.
C. Object is in java.lang and requires no import.
D. Designing and implementing inheritance hierarchies is a required AP CSA exam skill.
D. Designing inheritance hierarchies is explicitly excluded from AP scope. A, B, C are all true.
Consider the following code.

String a = "hello";
String b = a;
a = "world";

What does b hold after these three lines?
Trace what each variable holds after each line before choosing.
A. "world"
B. "hello"
C. null
D. "hello world"
B. After line 2, b holds the address of "hello". Line 3 makes a point to a new "world" object -- it does not affect b. b still references "hello".
A programmer sets Scanner sc = null; then calls sc.nextInt(). What happens?
A. A compile error.
B. Returns 0 because null acts as an empty Scanner.
C. A NullPointerException at run time.
D. Waits for keyboard input normally.
C. Calling an instance method on null compiles fine but throws NullPointerException at run time. There is no object to call the method on.
Which of the following correctly explains what a reference variable holds?
A. A copy of the object’s data directly in the variable’s memory location.
B. The class definition used to create the object.
C. The method signatures available on that type.
D. The memory address of the object, not the object’s data itself.
D. Reference variables hold addresses. The object data lives in heap memory at that address. This distinguishes reference types from primitives which hold values directly.
PRACTICE WITH A GAME — CHOOSE ONE:

Output Predictor — Objects and References

Type the exact output.

Question 1 of 6Score: 0

Bug Hunt — Objects and References

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

Bug 1 of 7Caught: 0

Tier 3 · AP Mastery Challenge

The Library System

A library uses a Book class with attributes title (String), pages (int), and methods getTitle() and isLong().

Part A

A student declares Book novel;. Which is true about novel?
A. novel holds an empty Book with default attribute values.
B. novel holds null because no object has been created yet.
C. A compile error occurs because all reference variables must be initialized.
D. novel holds 0 because int fields default to 0.
B. Unassigned reference variable = null. No Book object exists yet.

Part B

Two Book variables reference the same Book object. What is this called?
A. Aliasing — two reference variables pointing to the same object.
B. Overloading — two methods with the same name on one object.
C. Casting — converting the object type between the two variables.
D. Instantiation — creating two objects from the same class.
A. Aliasing = two+ reference variables pointing to the same object. Changes to the object via one are visible through the other.

Part C: Structured response

Explain the difference between a class and an object using Book as your example. Include: what the class defines, what the object is, and why multiple different objects can come from one class.

Extension · Beyond the Exam

Heap vs Stack memory

Primitive variables live on the call stack. Objects are allocated on the heap. Reference variables on the stack hold the heap address. When a method returns, its stack frame is discarded, but heap objects persist until garbage collection. This explains why aliasing works: two stack variables can point to the same heap object.

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]