AP Computer Science A Unit 5: Writing Classes
Study class design, constructors, accessor/mutator methods, scope, this keyword with exam-format practice and rubric-based scoring.
Start AP CSA Practice · Full Study Guide
AP and Advanced Placement are trademarks of College Board. AimFive is not affiliated with or endorsed by College Board.
Inside This Unit: The Full Breakdown
This unit teaches you to define your own classes: designing instance variables, writing constructors, creating methods, and understanding encapsulation and scope.
Why it matters
Writing classes is the essence of object-oriented programming. The AP CSA exam free-response section always includes a question requiring you to write a complete class or add methods to an existing one. Understanding encapsulation, this keyword, and method design is critical.
Key concepts
- Instance variables (fields) store the state of an object; they should be declared private for encapsulation.
- Constructors initialize instance variables when an object is created with new.
- Accessor methods (getters) return information; mutator methods (setters) modify state.
- The this keyword refers to the current object and distinguishes instance variables from parameters.
Designing a Class
A class defines a new type by specifying its data (instance variables) and behavior (methods). Instance variables should be private to enforce encapsulation — outside code cannot directly access or modify them. Public methods provide controlled access. A well-designed class has clear, single-purpose methods with descriptive names. The AP exam evaluates whether your class design properly encapsulates data and whether your methods correctly manipulate instance variables.
Constructors
A constructor has the same name as the class and no return type. It initializes instance variables when an object is created. A class can have multiple constructors with different parameter lists (overloading). If no constructor is written, Java provides a default no-argument constructor that sets fields to default values (0 for int, null for objects, false for boolean). Once you write any constructor, the default disappears. Use the this keyword when parameter names shadow instance variable names: this.name = name.
Methods and Scope
Methods define what objects can do. Accessor methods return information about the object's state without changing it. Mutator methods modify instance variables. Static methods belong to the class, not to any instance, and cannot access instance variables. Local variables declared inside a method exist only during that method call. Parameters are local variables initialized with the argument values. The scope of a variable determines where it can be used — instance variables are accessible throughout the class, while local variables are accessible only within their method.
AP exam tip
On the AP free response, always declare instance variables as private and provide public methods. If the problem says "write a method," include the full method header with return type, name, and parameters.
Connections to other units
- Unit 1: Objects are instances of the classes you write — the constructor creates them.
- Unit 6: Classes often contain arrays as instance variables to manage collections of data.
- Unit 9: Inheritance extends existing classes with new functionality using subclasses.