How do static variables and methods belong to the class rather than to individual objects?
Topic 5.7 Static Variables and Methods: declare and use static variables and methods, which belong to the class as a whole rather than to any one object, and explain how they differ from instance members.
A focused answer to AP CSA Topic 5.7, covering the static keyword, class-level variables shared by all objects, static methods called on the class, why static methods cannot use instance variables, and counting objects with a static field, 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.7) wants you to declare and use static variables and methods. A static member belongs to the class as a whole, not to any individual object: there is exactly one shared copy. Static variables are useful for data common to all objects (such as a count of how many have been created); static methods are utilities called on the class itself (like the Math methods). You must know how they differ from instance members and why a static method cannot use instance data.
Static variables belong to the class
public class Player {
private static int playerCount = 0; // one copy, shared by all Players
private String name; // each Player has its own name
}
So a static counter incremented in the constructor tracks the total number of objects ever created, because every constructor call touches the same shared variable.
Static methods belong to the class
public static int getPlayerCount() {
return playerCount; // uses only the static variable
}
// called as: Player.getPlayerCount()
Why a static method cannot use instance data
This is the most tested subtlety: an instance variable like name has no meaning inside a static method, because the method is not running on any one object.
Try this
Q1. How many copies of a static variable exist for a class with five objects? [1 point]
- Cue. One - a static variable is shared by the whole class, regardless of how many objects exist.
Q2. Explain why a static method cannot directly use an instance variable. [2 points]
- Cue. A static method belongs to the class and runs without any particular object, so there is no instance whose variable it could read; it can use only static members and its parameters.
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. Consider the following class.
```java
public class Widget {
private static int total = 0;
public Widget() {
total = total + 1;
}
public static int getTotal() {
return total;
}
}
Widget a = new Widget();
Widget b = new Widget();
Widget c = new Widget();
System.out.println(Widget.getTotal());
```
What is printed?
(A) `3`
(B) `1`
(C) `0`
(D) `9`
(E) Nothing; getTotal cannot be called on the class name.
Show worked answer →
The answer is (A).
total is a static variable, so there is one shared copy for the whole class, not one per object. Each call to the constructor increments that single shared total. Creating three Widgets makes total 1, then 2, then 3. getTotal() is a static method, correctly called on the class name Widget, and returns 3. (B) treats total as an instance variable; (E) is wrong because static methods are called on the class.
Markers reward knowing a static variable is shared across all objects and that a static method is called on the class.
AP 2020 (style)4 marksFree response (code writing). Write a class `Robot` that keeps a count of how many Robot objects have been created. Use a private static int instance counter, increment it in the constructor, and provide a public static method `count` that returns the current number of Robots created.
Show worked answer →
A 4-point question testing a static counter.
public class Robot {
private static int created = 0;
public Robot() {
created = created + 1;
}
public static int count() {
return created;
}
}
Point 1: a private static int created shared by all objects, initialised to 0. Point 2: the constructor increments the shared counter on each creation. Point 3: a public static method count. Point 4: it returns the shared created. Declaring created without static would give each object its own copy and break the count.
Related dot points
- 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 5.6 Writing Methods: design and implement methods that take parameters, perform a computation possibly using the object's instance variables, and return a result or perform an action.
A focused answer to AP CSA Topic 5.6, covering the method header (modifiers, return type, name, parameters), how a method uses instance variables and parameters, returning a value versus void, calling one method from another, and tracing a method, with a fully worked example.
- 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.
- 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 2.9 Using the Math Class: call the static Math methods in the AP Java subset (abs, pow, sqrt, random) and generate a random integer or double in a specified range.
A focused answer to AP CSA Topic 2.9, covering the required static Math methods abs, pow, sqrt and random, why Math methods are called on the class, the return types, and the standard formula for generating a random int in a range, with a worked trace.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)