From the free course AP Computer Science A on Lykke
3.7 Class Variables and Methods
Key concepts: Class variables (static variables) · Class methods (static methods) · Accessing static members with the class name and dot operator · Difference between class members and instance members · Accessor methods · Mutator (modifier) methods · The final modifier · Methods as behaviors of a class · Variables storing object data · Class structure: header, instance variables, constructors, and methods
A class variable belongs to the class itself, so every object created from that class shares one stored value. An instance variable, by contrast, belongs to one particular object.
3.7 Class Variables and Methods
A class variable belongs to the class itself, so every object created from that class shares one stored value. An instance variable, by contrast, belongs to one particular object. That distinction answers a practical question: should this information describe each object separately, or should it describe the entire group?
Imagine a library. Each Book object can store its own title and author, but the library may also need one shared count of how many books have been registered. A title is instance data; the total count is class data.
Class variables: one shared value
A class variable is declared with the keyword static. Because it is associated with the class rather than with individual objects, all instances observe the same variable. It is commonly accessed with the class name, followed by the dot operator.
public class LibraryBook {
private String title; // instance variable
private static int totalBooks = 0; // class variable
private static final int MAX_SHELF = 500; // class constant
public LibraryBook(String title) {
this.title = title;
totalBooks++;
}
public String getTitle() {
return title; // accessor for instance data
}
public void rename(String newTitle) {
title = newTitle; // mutator for instance data
}
public static int getTotalBooks() {
return totalBooks; // accessor for class data
}
public static void resetCount() {
totalBooks = 0; // mutator for class data
}
public static int shelfLimit() {
return MAX_SHELF;
}
}
If the program creates first and second, both objects contribute to the same totalBooks:
LibraryBook first = new LibraryBook("Dune");
LibraryBook second = new LibraryBook("Kindred");
System.out.println(LibraryBook.getTotalBooks()); // 2
The call uses LibraryBook.getTotalBooks(), not an object variable, because getTotalBooks is a class method. The method reads shared class data and does not need a particular book to perform its job.
Class methods and instance methods
A class method, also called a static method, is declared with static and belongs to the class. It can directly use class variables and call other class methods. It cannot directly use an instance variable such as title, because a static method has no particular object whose title it should choose.
An instance method belongs to an object and can work with that object’s instance variables. For example, first.getTitle() obtains the title stored in first, while LibraryBook.getTotalBooks() obtains the shared count.
| Member | Belongs to | Typical access | Can directly use |
|---|---|---|---|
| Instance variable | One object | objectName.member inside permitted access |
That object’s instance and class data |
| Class variable | The class | ClassName.member |
Shared class data |
| Instance method | One object | objectName.method() |
Instance and class data |
| Class method | The class | ClassName.method() |
Class data, not object-specific instance data |
Misconception check — “static means every object gets a starting copy.”
False. A static variable has one shared value for the class, not one separate value per object. Changing it through class code changes what every instance sees.
Accessors, mutators, and constants
An accessor method is a non-void method that returns information. getTitle accesses an object’s title, and getTotalBooks accesses shared class information. Returning the value through a method allows the class to control how its data is exposed.
A mutator, or modifier, method changes an object’s or class’s data. rename changes one book’s title; resetCount changes the class-wide count. A mutator is often void, although its essential feature is the change it makes, not merely its return type.
When a variable is declared final, its value cannot be modified after initialization. MAX_SHELF represents a class-level constant: every LibraryBook uses the same limit, and no method can assign it a new value.
The class as a design boundary
A Java class is organized around a class header, variables, constructors, and methods. Variables may store primitive values such as int, or reference values that refer to objects such as String. Methods define what instances can do and what other code can do with them.
Keeping variables private and exposing carefully designed accessors and mutators supports encapsulation: the class hides its internal representation while providing a controlled interface. This is the design reasoning assessed by 1.A: Determine an appropriate program design to solve a problem or accomplish a task, and the implementation of that design is assessed by 2.B: Write program code involving data abstractions.
Retrieval check: A class has three objects, but one shared static int activeCount. If the value changes from 3 to 4, how many separate copies changed? Why must a static method use class data rather than directly reading an instance variable?

Source Materials
- AP Computer Science A Course and Exam Description
- AP Computer Science A Course and Exam Description Clarifications and Corrections
- AP Computer Science A Course Overview
- AP Computer Science A Course at a Glance
- AP Computer Science A Course at a Glance Poster
- Adopt AP Computer Science A
- AP Computer Science A
- AP Computer Science A