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.

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 new keyword.
  • 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.
  • Object is the superclass of every class in Java (Gosling et al., 2025, §4.3.2), so every object has access to methods such as toString(), equals(Object), and getClass().

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

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();
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

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
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

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
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

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);

Hacks & Practice Tasks

Popcorn Hack

2-minute challenge:

  1. Finish Book by adding printInfo().
  2. Create one Book object, set its fields, and call printInfo().
// 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 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, because d1 was never changed
  • B. "Kairo", because d1 and d2 point to the same object
  • C. A compile error
  • D. The original value of d1.name before d2 was 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. e1 and e2 refer to the same object.
  • B. e1 and e2 refer to two separate objects.
  • C. e2 is a copy of e1’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, because a and b point 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);

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

past homework

All Java lessons