Skip to main content
United StatesComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. The class header
  3. Instance variables
  4. Constructors and methods
  5. Encapsulation
  6. Try this

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 private means 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

Sources & how we know this