Lesson 3.3: Anatomy of a Class
Lesson 3.3: Anatomy of a Class
What You'll Learn
- Identify all components of a Java class
- Write correct accessor and mutator methods
- Apply public and private access modifiers correctly
- Recognize common accessor/mutator errors on the AP exam
Key Vocabulary
| Term | Definition |
|---|---|
| class header | First line of a class definition; includes access modifier, class keyword, and class name |
| instance variable | Private attribute defined in the class body, outside any method; each object has its own copy (EK 3.3.A.1) |
| accessor (getter) | Method that returns the value of a private instance variable without modifying it (EK 3.3.A.3) |
| mutator (setter) | Void method that modifies the value of a private instance variable (EK 3.3.A.4) |
| encapsulation | Bundling private data with public methods that control access (EK 3.3.A.5) |
| public | Access modifier: the member can be accessed by any class |
| private | Access modifier: the member can only be accessed within its own class (EK 3.3.A.5–3.3.A.6) |
CED EK 3.3.A.1–3.3.A.6
A class contains private instance variables (EK 3.3.A.1), accessor methods for read access (EK 3.3.A.3), and mutator methods for write access (EK 3.3.A.4). Instance variables should be private (EK 3.3.A.5). Public methods are accessible anywhere; private methods only inside the class (EK 3.3.A.6).
Complete Class Anatomy
public class Rectangle { // class header
private double width; // instance variable
private double height;
public Rectangle(double w, double h) { // constructor
width = w; height = h;
}
public double getWidth() { return width; } // accessor
public double getHeight() { return height; }
public void setWidth(double w) { // mutator
if (w > 0) width = w;
}
public double getArea() { return width * height; } // behavior
}
Accessor Methods: Rules
Standard Accessor Pattern
public String getName() {
return name;
}
Convention: get + capitalized variable name. Return type must match the variable type. NEVER modifies the variable.
Mutator Methods: Rules
Standard Mutator Pattern
public void setName(String n) {
name = n;
}
Almost always void. Takes a parameter of the variable's type. Can include validation.
Access Modifier Summary
| Member | Typical Modifier |
|---|---|
| Instance variables |
private -- protects state (EK 3.3.A.5) |
| Constructor |
public -- allows object creation from outside |
| Accessor methods |
public -- external read access |
| Mutator methods |
public -- external write access (controlled) |
| Helper methods |
private -- internal use only |
AP Trap: Getter Must NOT Modify State
// WRONG
public int getCount() { return count++; }
A getter that modifies a variable violates the accessor contract. The AP exam shows this pattern and asks why it is incorrect.
AP Trap: Return Type Must Match
If balance is a double, the getter MUST return double, not int.
AP Trap: Setter Should Be void
// WRONG
public int setAge(int a) { age = a; return age; }
// CORRECT
public void setAge(int a) { age = a; }
Setters typically return void. Returning a value from a setter is non-standard and a common distractor.
AP Trap: Accessing Private Variable Externally Is a Compile Error
// In another class: myAccount.balance = 10000.0; // COMPILE ERROR
Private restricts external access. This is a compile-time error, not a runtime exception.
Real-World Connection: A thermostat exposes a public interface (setTemperature(), getCurrentTemp()) while keeping sensor calibration private.
Summary
- A class has a header, private instance variables, constructors, and methods.
- Instance variables should always be private (EK 3.3.A.5).
- Accessors return variable values and must NOT change state.
- Mutators are typically void and modify a private variable.
- Public methods accessible anywhere; private methods only inside the class (EK 3.3.A.6).
- Return type of a getter must exactly match the type of the variable it returns.
Practice Questions
public boolean isOn() { return isOn; }
public void turnOn() { isOn = true; }
public int getScore() { score += 10; return score; }
private double reading;, which is the CORRECT mutator?public double setReading(double r) { reading = r; return reading; }
private void setReading(double r) { reading = r; }
public void setReading(double r) { reading = r; }
public void getReading(double r) { reading = r; }
private String title;, which accessor is CORRECT?public void getTitle() { return title; }
public String getTitle() { return title; }
public String getTitle() { title = ""; return title; }
private String getTitle() { return title; }
public class Box {
private int length; private int width;
public void setLength(int l) { length = l; }
public int getLength() { return length; }
public int getArea() { return length * width; }
}
myAccount.balance = 10000.0; where balance is private. Result?private?II. The constructor should be public
III. Accessor methods should be
public voidWhich are TRUE?
Mastery: Anatomy of a Class
public class Patient {
public String name;
private int age;
public Patient(String n, int a) { name = n; age = a; }
public String getName() { return name; }
}
public class Bottle {
private String liquid;
private void Bottle(String l) { liquid = l; }
public String getLiquid() { return liquid; }
}
private int temperature;, which accessor/mutator pair is CORRECTLY defined?public void getTemp() with return + public int setTemp(int t)
public int getTemp() { return temperature; } + public void setTemp(int t) { temperature = t; }
public int getTemp() { temperature++; return temperature; } + void setter
private int getTemp() { return temperature; } + void setter
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]