01 / 21

Objects
Instances of Classes

Learn how classes serve as blueprints and how objects bring them to life as individual instances in Java.

AP Computer Science A

What Is an Object?

In Java, an object is a specific instance of a class. It exists in memory, holds its own data (state), and can perform actions (behavior). Think of it as a real, tangible thing built from a template.

What Is a Class?

A class is a blueprint or template that defines what data an object will store and what actions it can perform. It specifies instance variables (state) and methods (behavior), but it does not occupy memory as an object itself.

State
Instance variables that hold data unique to each object (e.g., name, age).
Behavior
Methods that define what an object can do (e.g., speak(), move()).

From Blueprint to Objects

class Dog name breed age bark() fetch() BLUEPRINT new dog1 "Buddy" "Golden", 3 dog2 "Luna" "Poodle", 5 dog3 OBJECTS (instances)

One ticket office counter, many fixed ticket IDs

One ticket agent hands a new stub to a patron while another patron retains an issued stub.Separate tickets provide an analogy for separate objects. In the Dog example, each instance has its own name and age fields.

Each object keeps its own state

Separate Dog object boxes contain name Milo with age 2 and name Luna with age 4.Suppose two Dog instances store Milo, age 2, and Luna, age 4. Each name and age belongs to its own instance. Changing one object’s fields does not automatically change the other object.

Creating Objects with new

In Java, you create an object using the new keyword followed by a call to the class's constructor. This allocates memory and initializes the object.

// Declare and create Dog myDog = new Dog("Buddy", "Golden", 3);

The Constructor

A constructor is a special method that runs when an object is created. It has the same name as the class and no return type. Its job is to initialize instance variables.

public class Dog { private String name; private int age; // Constructor public Dog(String n, int a) { name = n; age = a; } }

Reference vs. Object

STACK (References) dog1 dog2 HEAP (Objects) Dog: "Buddy", 3 @0x3a7f Dog: "Luna", 5 @0x5c2d

Class vs. Object: Key Differences

Class Object
Definition Blueprint / template Instance of a class
Memory No memory allocated Allocated on the heap
Created with class keyword new keyword
Quantity Defined once Many can exist
Holds data? Defines structure only Stores actual values

Understanding null References

A reference variable can be set to null, meaning it points to no object. Calling a method on a null reference causes a NullPointerException at runtime.

Dog myDog = null; // no object created myDog.bark(); // NullPointerException! // Fix: create the object first myDog = new Dog("Rex", 2); myDog.bark(); // Works fine

How to Create and Use an Object

1
Define the class with instance variables and methods (or use an existing class).
2
Declare a reference variable of the class type: ClassName varName;
3
Instantiate with new: varName = new ClassName(args);
4
Call methods on the object using dot notation: varName.methodName();
5
Access return values or modify state through the object's public interface.

Example 1: Simple Object Creation

public class Student { private String name; private double gpa; public Student(String n, double g) { name = n; gpa = g; } public String getName() { return name; } public double getGpa() { return gpa; } public void setName(String newName) { name = newName; } public void setGpa(double newGpa) { gpa = newGpa; } }
// In main method: Student s1 = new Student("Alice", 3.9); System.out.println(s1.getName()); // Alice System.out.println(s1.getGpa()); // 3.9

Example 2: Multiple Objects, One Class

Student s1 = new Student("Alice", 3.9); Student s2 = new Student("Bob", 3.5); Student s3 = new Student("Carol", 3.7); System.out.println(s1.getName()); // Alice System.out.println(s2.getName()); // Bob System.out.println(s3.getGpa()); // 3.7

Example 3: Reference Aliasing

Student a = new Student("Alice", 3.9); Student b = a; // b and a point to the SAME object b.setGpa(4.0); System.out.println(a.getGpa()); // 4.0 (changed!)
a b Student: "Alice" gpa = 4.0

Two collaborators work on one shared model

Two collaborators adjust one blue articulated model while a second model stands apart.Two variables can provide access to one shared object. Copying a reference creates another access path; it does not duplicate the object.

Follow object identity

References d1, d2 and d3 are traced through two distinct Dog objects and a null value.Two new expressions create Fido and Rex. After the assignments, d1 refers to Rex, d3 refers to Fido and d2 is null. Both objects remain reachable; == compares reference identity, while equals depends on the class’s implementation.

Calling Methods with Dot Notation

Once you have an object reference, you invoke its methods using dot notation: the reference name, a dot, then the method name with parentheses.

String greeting = new String("Hello, World!"); // Calling methods on the String object int len = greeting.length(); // 13 String upper = greeting.toUpperCase(); // "HELLO, WORLD!" char ch = greeting.charAt(0); // 'H'

Common Mistakes to Avoid

✕ Forgetting new
Writing Dog d = Dog("Rex", 2); without new causes a compile error. The new keyword is required to instantiate.
✕ Calling on null
Declaring a reference without assigning an object, then calling a method: NullPointerException at runtime.
✕ Confusing class for object
Trying to use a class name to call non-static methods directly: Dog.bark() won't work unless bark is static.

Summary: Key Concepts

Concept Definition Example
Class Blueprint defining state and behavior class Dog { ... }
Object Instance created from a class new Dog("Rex", 2)
Constructor Special method that initializes objects public Dog(String n, int a)
Reference Variable storing an object's address Dog d = ...
null Reference pointing to no object Dog d = null;

Question 1 of 5

Which keyword is used to create a new object in Java?

Question 2 of 5

Given the Student class from the examples, what is the output of the following code?

Student a = new Student("Alice", 3.9); Student b = a; b.setName("Max"); System.out.println(a.getName());

Question 3 of 5

What happens when you execute: Dog d = null; d.bark();?

Question 4 of 5

Which statement about constructors is correct?

Question 5 of 5

Consider:
Student s1 = new Student("A", 3.5);
Student s2 = new Student("A", 3.5);
How many objects exist in memory?