Skip to main content
United StatesComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.811 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. Static variables belong to the class
  3. Static methods belong to the class
  4. Why a static method cannot use instance data
  5. Try this

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

Sources & how we know this