Unit 5: Writing Classes

AP Computer Science A: 11 practice questions with detailed explanations.

Unit Study Guide

Executive Summary

Classes are blueprints for objects. Instance variables store each object's state; methods define behavior; constructors set initial state.

Anatomy of a class

Instance variables (private) hold state. Constructors (same name as the class, no return type) initialize them. Methods have a return type or void, a name, and parameters. private members are visible only inside the class; public members are visible everywhere.

Methods and parameters

Formal parameters are the names in the signature; actual parameters are the values at the call. Primitives pass by value — changes inside the method do not affect the caller. Object references also pass by value, but the copy points to the same object, so its contents CAN change.

this and overloading

this refers to the current object and disambiguates instance variables from parameters. Overloading lets methods share a name when their parameter lists differ. Return type alone never distinguishes overloads.

Static members

static methods and variables belong to the class, not instances. Static methods cannot use instance variables directly and are called as ClassName.method(). Constants use public static final.

Exam traps

A missing return on one path fails to compile. Accessors return values; mutators change state. Getters/setters protect private fields (encapsulation).

Top 5 Concepts to Master

  1. 1Write classes with private fields and public methods.
  2. 2Trace parameter passing for primitives and objects.
  3. 3Identify legal overloads.
  4. 4Decide when members should be static.

Key Terms & Definitions

Practice with Flashcards
Instance variable

Field storing one object’s state.

Constructor

Method that initializes a new object.

this

Reference to the current object.

Overloading

Same method name with different parameter lists.

static

Belongs to the class, shared by all instances.

Accessor

Method that returns a field’s value (getter).

Mutator

Method that changes a field (setter).

Encapsulation

Hiding fields behind private and public methods.

Common Misconceptions: Exam Traps

Return type is part of the method signature for overloading.

Correct: Overloads differ by parameters only.

Objects are passed by reference, so reassigning the parameter changes the caller.

Correct: The reference itself is copied; reassignment is local.

Static methods can freely use instance variables.

Correct: Static context has no this, so instance fields are off-limits.

Constructors can be void.

Correct: Constructors have no return type at all.

Question Bank Breakdown

By difficulty

easy 11

By topic

Static Methods and Variables 4Class Structure 4Methods 2Access Modifiers 1

All Questions in this Unit