| Study Guides
Computer Science A · AP CS A CED Unit 5 · 16 min read · Updated 2026-05-11

Writing Classes — AP Computer Science A

AP Computer Science A · AP CS A CED Unit 5 · 16 min read

1. Class Anatomy and Core Components ★★☆☆☆ ⏱ 5 min

Every Java class is a blueprint for objects that bundles state (data) and behavior (methods) into a single reusable unit, the foundation of object-oriented programming in Java. All standard classes have three core components.

  1. **Fields (Instance Variables)**: Variables declared inside the class; each object gets its own copy to store individual state.
  2. **Constructors**: Special methods that run when an object is created with `new`. No return type (not even `void`), name must exactly match the class name. Java provides a default no-argument constructor only if no custom constructors are written.
  3. **Methods**: Functions inside the class that define object behavior; can read/modify state, take parameters, and return values.

Exam tip: Examiners regularly deduct marks for mismatched constructor and class names, or adding a `void` return type to constructors. Double-check these details before submitting your FRQ answer.

2. Static vs Instance Members ★★★☆☆ ⏱ 4 min

All class members (fields and methods) are either instance (belonging to individual objects) or static (belonging to the entire class). This distinction is heavily tested in both MCQ and FRQ sections of the AP CS A exam.

Exam tip: While Java allows calling static methods on object references, this is poor practice and will lose marks on the exam. Always use the class name to call static methods.

3. Encapsulation with `private` Access Modifier ★★☆☆☆ ⏱ 3 min

Encapsulation is a core object-oriented principle that restricts direct access to an object's internal state, allowing you to control modifications, prevent invalid state, and hide implementation details from external code.

If `fullName` were public, external code could set it to `null` or an empty string, creating an invalid object state. Encapsulation eliminates this risk.

Exam tip: Over 90% of AP CS A class writing FRQs require all fields to be private. Leaving a field public will cost you at least 1 mark per field, with no exceptions.

4. Method Overloading ★★★☆☆ ⏱ 4 min

Method overloading lets you define multiple methods with the same name in the same class, for different use cases. Java selects the correct method to run based on the method's parameter list.

\text{New GPA} = \frac{(\text{Current GPA} \times \text{Total Completed Credits}) + (\text{Course Grade} \times \text{New Credit Hours})}{\text{Total Completed Credits} + \text{New Credit Hours}}

Exam tip: Examiners regularly test the difference between overloading and overriding. Overloading occurs in the same class, same name, different parameters. Overriding occurs in a subclass, same name, identical parameters.

5. Documentation and Comments ★☆☆☆☆ ⏱ 2 min

Clear documentation improves code readability, and well-placed comments can help you earn partial credit on FRQs even if your code has small errors. Java supports three main comment types for different uses.

  1. **Single-line comments**: `//` for short notes explaining logic inside methods.
  2. **Multi-line comments**: `/* ... */` for longer notes spanning multiple lines.
  3. **Javadoc comments**: `/** ... */` for official documentation of classes, constructors, and methods, using tags like `@param` and `@return`.

You are not required to write full Javadoc on the AP exam unless explicitly asked, but adding short single-line comments for non-obvious logic helps graders follow your reasoning and award partial credit for partially correct code.

Common Pitfalls

Why: Students confuse constructors with regular methods, which always require a return type.

Why: Direct field access is faster to write, and many students forget that private fields are not accessible outside the class.

Why: Students mix up which members belong to the entire class vs individual objects.

Why: Students incorrectly assume return type is part of the method signature for overloading.

Why: Students forget the default constructor is only automatically provided when no custom constructors are defined.

Quick Reference Cheatsheet

← Back to topic

Stuck on a specific question?
Snap a photo or paste your problem — Ollie (our AI tutor) walks through it step-by-step with diagrams.
Try Ollie free →