What are the parts of a Java class, and how do instance variables, constructors and methods fit together?
Topic 5.1 Anatomy of a Class: identify the parts of a class definition (the class header, private instance variables, constructors and methods) and explain encapsulation through access modifiers.
A focused answer to AP CSA Topic 5.1, covering the class header, private instance variables, constructors and methods, the meaning of public and private, encapsulation, and how the pieces combine into a working class, with a fully worked example.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this topic is asking
The College Board (Topic 5.1) wants you to know the anatomy of a class: the parts that make up a class definition and how they work together. A class is the blueprint from which objects are built. Its parts are the class header, private instance variables (the object's data), constructors (which set up new objects), and methods (the behavior). Understanding encapsulation - keeping data private and exposing controlled access - is the design idea that ties it all together.
The class header
public class Student {
// body: instance variables, constructors, methods
}
The header names the class and its access level. By convention class names are capitalized (Student, BankAccount). The body, enclosed in braces, holds everything else.
Instance variables
public class Student {
private String name; // each Student has its own name
private int grade; // and its own grade
}
Constructors and methods
A constructor has the same name as the class and no return type; it runs when an object is created with new, setting the initial values of the instance variables (Topic 5.2). Methods define what an object can do - accessors that report data, mutators that change it, and other behavior (Topics 5.4 to 5.6).
Encapsulation
Encapsulation lets the class guarantee its data stays valid: a mutator can reject bad values, and the internal representation can change without breaking outside code. public and private are the access modifiers that make this possible (Topic 5.8).
Try this
Q1. Name the four kinds of members that typically make up a class definition. [1 point]
- Cue. The class header, instance variables, constructors, and methods.
Q2. Explain what encapsulation means and how private instance variables support it. [2 points]
- Cue. Encapsulation bundles data with the methods that use it and hides the data from outside code; declaring instance variables
privatemeans they can only be accessed through the class's own methods, so the class controls and protects its state.
Exam-style practice questions
Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AP 2021 (style)1 marksMultiple choice. In a well-designed class, why are instance variables typically declared `private`?
(A) To enforce encapsulation, so they can only be accessed or changed through the class's own methods.
(B) Because `private` variables run faster than `public` ones.
(C) Because a class is not allowed to have `public` variables.
(D) So that they are shared by every object of the class.
(E) So that they cannot be used inside the class's own methods.
Show worked answer →
The answer is (A).
Declaring instance variables private is the core of encapsulation: outside code cannot read or modify the data directly, only through the accessor and mutator methods the class chooses to provide. This lets the class protect its state and validate changes. (B) is false; access modifiers do not affect speed. (C) is false; public variables are legal but poor design. (D) describes static, not private. (E) is false; the class's own methods can always use its private variables.
Markers reward naming encapsulation and explaining that private restricts access to the class's own methods.
AP 2020 (style)4 marksFree response (code writing). Write the complete declaration of a class `Book` with two private instance variables: a `String` called `title` and an `int` called `pages`. Include only the class header and the instance variable declarations (no constructor or methods yet). Use proper access modifiers.
Show worked answer →
A 4-point question testing the skeleton of a class.
public class Book {
private String title;
private int pages;
}
Point 1: the class header public class Book with the correct keyword and capitalized class name. Point 2: a private String title instance variable. Point 3: a private int pages instance variable. Point 4: correct syntax with braces enclosing the body and semicolons ending each declaration. Declaring the variables public, or omitting private, would lose the encapsulation mark.
Related dot points
- Topic 5.2 Constructors: write constructors that initialise an object's instance variables from parameters, including overloaded and default constructors, and explain the default initial values.
A focused answer to AP CSA Topic 5.2, covering constructor syntax, initialising instance variables from parameters, the no-argument constructor, overloading, default values when a variable is not set, and the difference between a parameter and an instance variable, with a fully worked example.
- Topic 5.4 Accessor Methods: write accessor (getter) methods, including a toString method, that return information about an object's state without changing it.
A focused answer to AP CSA Topic 5.4, covering accessor (getter) methods, the non-void return type, why accessors do not change state, the toString method and how println uses it, and returning computed values, with a fully worked example.
- Topic 5.5 Mutator Methods: write mutator (setter) methods, usually void, that change an object's instance variables, including methods that validate or constrain the new value before assigning it.
A focused answer to AP CSA Topic 5.5, covering mutator (setter) methods, the void return type, changing instance variables from a parameter, validating a new value before assigning, and why mutators protect encapsulated data, with a fully worked example.
- Topic 5.8 Scope and Access: distinguish local variable scope from instance variable scope, and use the access modifiers public and private to control where a class member can be used.
A focused answer to AP CSA Topic 5.8, covering the scope of local variables and parameters versus instance variables, variable shadowing, the public and private access modifiers, and how access control supports encapsulation, with a fully worked example.
- Topic 2.1 Objects: Instances of Classes: explain the relationship between a class and its objects, and describe an object as an instance of a class with state and behavior.
A focused answer to AP CSA Topic 2.1, covering the class-object relationship, what it means for an object to be an instance, the difference between attributes (state) and methods (behavior), and reference versus primitive variables, with a worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)