Welcome to Unit 3: Class Creation!

In previous units, you learned how to use objects that someone else built for you (like a String or a Scanner). Now, it’s your turn to be the architect! In this unit, we are moving from being "users" to being "creators." We are going to learn how to write the blueprints—called Classes—that tell the computer how to build objects and what they can do.

Don’t worry if this feels like a big jump. We’ll take it one "brick" at a time!

3.1 Anatomy of a Class: The Blueprint

Think of a Class as a blueprint for a house. The blueprint itself isn't a house; it’s just the set of instructions. When you use that blueprint to actually build a house, you have created an Object (also called an Instance).

Every class you create will generally have three main parts:

  1. Instance Variables (What the object knows/attributes)
  2. Constructors (How the object is born)
  3. Methods (What the object can do/behaviors)

The Concept of Encapsulation

Imagine your smartphone. You can use the screen and buttons (the public interface), but you can't see the messy wires inside (the private data). This is called Encapsulation. In Java, we keep our data safe by making instance variables private and only allowing access through public methods. This keeps our code organized and prevents "accidents" from other parts of the program.

Quick Review: Always mark your instance variables as private! It’s one of the most common things graders look for on the AP Exam.

3.2 Instance Variables: The Data

Instance variables define the state of an object. If we were making a class for a VideoGameCharacter, the instance variables might be health, name, and level.

Example:
private String name;
private int health;

Key Term: State

The State of an object is simply the current values of its instance variables at any given moment. If a character’s health changes from 100 to 90, its "state" has changed.

3.3 Constructors: Bringing Objects to Life

A Constructor is a special piece of code that runs only once: when you use the new keyword to create an object. Its job is to set the starting values for your instance variables.

Rules for Constructors:

  1. It must have the exact same name as the Class.
  2. It does not have a return type (not even void).
  3. It is usually public.
Overloading Constructors

You can have more than one constructor in a class! This is called Constructor Overloading. This allows you to create objects in different ways. For example, you might have one constructor where you provide a name, and another where the name defaults to "Guest."

Did you know? If you don’t write any constructors at all, Java provides a "default constructor" for you that takes no parameters and sets everything to zero or null. But as soon as you write your own constructor, that free one disappears!

3.4 Documentation with Comments

When you create classes, you need to explain what they do. In AP CSA, we use Javadoc comments. These start with /** and end with */.

Inside these, you might see @param (to explain a variable coming into a method) and @return (to explain what the method sends back). Good documentation makes your code readable for humans, not just machines!

3.5 Accessor and Mutator Methods

Since we made our variables private, we need a way to interact with them safely. We use two types of methods:

1. Accessors (Getters)

These methods "get" a value for you to see. They return a value and usually start with the word "get."
Example: public int getHealth() { return health; }

2. Mutators (Setters)

These methods "mutate" or change the value. They usually take a parameter and are void because they don't return anything; they just do work.
Example: public void setHealth(int newHealth) { health = newHealth; }

Analogy: An Accessor is like looking at a thermometer to see the temperature. A Mutator is like turning the knob on the AC to change the temperature.

3.6 Static Variables and Methods

Sometimes, you want a variable or method to belong to the Class itself, not to any specific object. We use the keyword static for this.

The Scoreboard Analogy: Imagine a basketball game. Each player has their own "points scored" (instance variable). However, there is only one "Total Team Score" on the wall (static variable). Every player shares that same scoreboard!

Static vs. Non-Static
  • Non-Static: Unique to every object (e.g., each dog has its own name).
  • Static: Shared by all objects of that class (e.g., the species "Canine" is the same for all dogs).

3.7 Scope and the "this" Keyword

Scope refers to where a variable can be seen and used.

  • Local Variables: Declared inside a method. They die when the method ends.
  • Instance Variables: Declared at the top of the class. they live as long as the object lives.

The "this" Keyword

Sometimes, a parameter in a constructor has the same name as an instance variable. To tell Java which one is which, we use this to refer to the object we are currently in.

Example:
public Character(String name) {
this.name = name; // "this.name" is the instance variable, "name" is the parameter
}

Memory Aid: Think of this as pointing a finger at yourself. "I want this object's name to equal the name I was just given."

Common Mistakes to Avoid

  • Adding a return type to a constructor: If you write public void MyClass(), Java thinks it's a regular method, not a constructor!
  • Forgetting 'private': Always default to private for variables unless told otherwise.
  • Using 'static' for everything: Only use static if the value should be shared across every single object you create.
  • Confusing Parameters and Instance Variables: Remember that parameters are just temporary envelopes used to deliver data to the class.

Unit Summary Key Takeaways

1. Classes are Blueprints: They define what objects will look like and do.
2. Encapsulation: Keep data private; provide public methods to interact with it.
3. Constructors: They initialize the state of the object using the new keyword.
4. Methods: Accessors let us see data; Mutators let us change it.
5. Static: Use it for data or behaviors that belong to the whole class, not individual objects.

Keep practicing! Creating your first few classes can feel a bit like learning a new language, but once you understand the pattern (Variables -> Constructors -> Methods), you'll be building complex systems in no time!