Welcome to Unit 3: Building Your Own Blueprints!
Up until now, you have been a user of objects—you’ve used \( String \) objects, \( Scanner \) objects, and maybe even \( ArrayList \) objects. But now, you are moving into the role of an architect. In this chapter, "Anatomy of a Class," we are going to look at the skeletal structure of a Java class. Think of a class as a blueprint or a template. It defines what an object "knows" and what it can "do."
Don't worry if this seems like a lot to take in at first. Every programmer started exactly where you are, and once you see the pattern, it becomes second nature!
1. The Big Picture: What is a Class?
If we were designing a video game, we might need a class called \( Hero \). Every single "Hero" object in the game would be built from this same blueprint. The class tells Java:
- State: What data does the hero keep track of? (e.g., health, name, score)
- Behavior: What actions can the hero perform? (e.g., jump, take damage, heal)
2. The Class Header
Every class starts with a header. This is like the title page of a book. It tells Java the name of the class and who can access it.
Example:
\( public \space class \space Hero \)
{
// The rest of the code goes inside these curly braces!
}
Key Breakdown:
- \( public \): This is an access modifier. It means other classes can use this blueprint. For the AP Exam, your class headers will almost always be \( public \).
- \( class \): This is the keyword that tells Java "I am about to define a new template."
- \( Hero \): This is the name of your class. By convention, class names always start with a Capital Letter.
Quick Review: The Curly Braces
Notice the \( \{ \) and \( \} \). Everything that belongs to the class must stay inside these "arms." If you write code outside the closing brace, Java will get very confused!
3. Instance Variables (The "State")
Instance variables are variables defined inside a class but outside any method. They represent the data that each instance (object) of the class will hold.
The Golden Rule of Unit 3: Always make your instance variables \( private \).
Example:
\( private \space String \space name; \)
\( private \space int \space health; \)
Why \( private \)? This is a concept called encapsulation. By making variables private, we "protect" the data. Only the methods inside the \( Hero \) class can change the health or the name directly. It prevents outside code from accidentally breaking our object's data.
Did you know? These are called "instance" variables because every new instance of a class gets its own copy. If you have two \( Hero \) objects, one can have a \( health \) of \( 100 \) while the other has a \( health \) of \( 50 \).
4. The Components of the Body
Inside the class braces, we usually follow a specific order. While Java doesn't strictly require this order, it is the standard "anatomy" you will see in textbooks and on the AP Exam:
- Instance Variables: The data (Attributes).
- Constructors: The instructions for creating a new object (covered in Topic 3.4).
- Methods: The actions the object can take (covered in Topic 3.5).
Analogy: Imagine a "Car" blueprint.
1. Variables: color, fuelLevel, speed.
2. Constructor: "To build a Car, you need to pick a color and start the fuel at 100."
3. Methods: drive(), brake(), honk().
5. Formal Scope: Where can I see the variables?
In the anatomy of a class, Scope refers to where a variable "lives" and can be used.
- Instance Variables: Since they are declared at the top of the class, they can be used by any method inside that class.
- Local Variables: These are variables declared inside a specific method. They "die" as soon as that method finishes running.
Key Takeaway: If you want a piece of information to last for the entire life of the object, make it an instance variable.
6. Summary Checklist
When you are looking at or writing a class, check for these parts:
1. Class Header: Does it say \( public \space class \space Name \)?
2. Opening Brace: Is there a \( \{ \) at the start?
3. Private Variables: Are the variables declared as \( private \)?
4. Closing Brace: Is there a \( \} \) at the very end?
Common Mistakes to Avoid
- Forgeting \( private \): On the Free-Response Section of the AP Exam (Question 2), forgetting to mark instance variables as \( private \) can cost you points!
- Putting code outside braces: Make sure every method and variable is safely tucked inside the class's curly braces.
- Capitalization: Remember, \( class \space names \) start with Capitals, but \( variable \space names \) and \( method \space names \) start with lowercase letters (camelCase).
Note: For more details on how to actually initialize these variables, head over to the notes on Constructors (3.4). To learn how to make the objects do things, check out Methods (3.5)!