01 / 21

Anatomy of a
Java Class

Understand the building blocks of every Java program: classes, fields, constructors, and methods that work together as a blueprint for objects.

AP Computer Science A

What Is a Class?

A class is a blueprint or template that defines the attributes (data) and behaviors (actions) an object can have. Think of it like an architectural plan: the plan itself is not a house, but every house built from it shares the same structure.

Classes vs Objects

Class
The blueprint: defines what data and methods exist. Written once, used many times. Example: Dog class.
Object
An instance created from the blueprint. Each object has its own copy of instance variables. Example: myDog.

Why Classes Matter

1
Organization: group related data and behavior in one place instead of scattered variables and functions.
2
Reusability: define the blueprint once, then create as many objects as you need without rewriting code.
3
Encapsulation: hide internal details behind private fields and expose only what is necessary through public methods.
4
AP Exam focus: the College Board expects you to read, write, and trace code that uses classes, constructors, and methods.

The Four Parts of a Class

public class Dog INSTANCE VARIABLES (Fields) private String name; private int age; CONSTRUCTOR public Dog(String n, int a) { name = n; age = a; } METHODS public String getName() { return name; } public void bark() { ... } toString() / OTHER OVERRIDES

Instance Variables: The Data

Instance variables (also called fields) store the state of each object. Every object gets its own independent copy.

public class Circle { // instance variables private double radius; private String color; }

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.

Constructors: Building Objects

A constructor is a special method that initializes a new object. It has the same name as the class and no return type.

public class Circle { private double radius; // Constructor public Circle(double r) { radius = r; } // No-argument constructor public Circle() { radius = 1.0; } }

Methods: The Behavior

Methods define what an object can do. They can return values (accessor methods) or change the object's state (mutator methods).

Accessor (Getter)
Returns data without changing state. Has a return type matching the data returned.
Mutator (Setter)
Changes instance variable values. Return type is typically void.
// Accessor public double getRadius() { return radius; } // Mutator public void setRadius(double r) { radius = r; }

public vs private

public private
Visible to Any class Only the declaring class
Used for Constructors, methods Instance variables
Why Provide controlled access Enforce encapsulation
AP Exam default Methods & constructors All instance variables

The this Keyword

When a parameter name matches an instance variable name, Java needs a way to tell them apart. The keyword this refers to the current object.

public class Circle { private double radius; public Circle(double radius) { // this.radius = the field // radius = the parameter this.radius = radius; } }

The toString Method

Every class in Java inherits a toString() method from Object. You override it to return a human-readable String describing the object.

public String toString() { return "Circle with radius " + radius; } // Usage: Circle c = new Circle(5.0); System.out.println(c); // Prints: Circle with radius 5.0

Complete Example: Student Class

public class Student { // Instance variables private String name; private int grade; // Constructor public Student(String name, int grade) { this.name = name; this.grade = grade; } // Accessor public String getName() { return name; } // Mutator public void setGrade(int g) { grade = g; } // toString public String toString() { return name + " (Grade " + grade + ")"; } }

Using the Student Class

// Creating objects Student s1 = new Student("Alice", 11); Student s2 = new Student("Bob", 10); // Calling methods System.out.println(s1.getName()); // Alice s2.setGrade(11); System.out.println(s2); // Bob (Grade 11)
1
The new keyword calls the constructor and allocates memory for a new object.
2
Use dot notation (object.method()) to call methods on a specific object.
3
Printing an object calls toString() automatically.

How to Write a Class

1
Name the class: use a capitalized noun that describes the object (e.g., BankAccount, Rectangle).
2
Declare instance variables: list the data each object needs. Mark them all private.
3
Write constructor(s): initialize every instance variable. Consider a no-arg constructor with default values.
4
Add accessors and mutators: write getters for reading and setters for modifying private fields.
5
Override toString(): return a meaningful String representation of the object's state.

Common Mistakes to Avoid

✕ Return Type on Constructor
Writing void Dog() turns it into a regular method. Constructors have no return type, not even void.
✕ Public Instance Variables
Declaring fields as public breaks encapsulation. The AP exam expects private for all instance variables.
✕ Forgetting this
When parameter names match field names, this.field = param is required, or the field stays at its default value.

Class Anatomy Summary

Component Purpose Key Rule
Instance Variables Store object state private
Constructor Initialize new objects Same name as class, no return type
Accessor Method Return field values Has a return type, no side effects
Mutator Method Modify field values Usually returns void
toString() String representation Called automatically by println

Question 1 of 5

Which of the following is true about constructors in Java?

Question 2 of 5

What does System.out.println(new Pet("Max", 3)) print?

Question 3 of 5

What is the primary purpose of declaring instance variables as private?

Question 4 of 5

Which type of method changes the value of an instance variable?

Question 5 of 5

What happens when the last line executes?