3.7 Class Variables and Methods
💻AP Computer Science A Unit 3 Review
3.7 Class Variables and Methods
💻 AP Computer Science A
TLDR
Static variables and methods belong to the class itself, not to any single object. A static (class) variable is shared by every object of the class because there is only one copy, and a static (class) method runs without needing a specific object. In AP Computer Science A, you mainly need to declare class variables with static, access them with the class name, and know that class methods cannot touch instance variables or call instance methods unless you pass them an object.
Free-response class design problems can ask for behavior that works at the class level, so you may need to write a static method as well as a shared static variable. Multiple-choice questions can also ask why a class method cannot reach an instance variable.
Shared Static Variables and Class Method Limits
- A class variable uses the
statickeyword and is shared by every object of the class, so all objects see and change the same single copy. - Public class variables are accessed from outside the class with the class name and the dot operator, like
ClassName.variable, because they belong to the class, not to an object. - A class method (static method) can read and change class variables and call other class methods.
- A class method cannot access instance variables or call instance methods unless it receives an object of the class as a parameter.
- Declaring a variable
finalmeans its value cannot be changed after it is set, which is how you make a constant.
Core Concepts
Class Level vs Instance Level
| Storage | Level | name value | Shared count / trace |
|---|---|---|---|
| Student Alice | Instance | "Alice" | → Student.totalStudents |
| Student Bob | Instance | "Bob" | → Student.totalStudents |
Student.totalStudents | Class (static) | — | One shared value: 2 |
| Construction trace | — | Alice, then Bob | 0 → 1 → 2 |
Every value in a class lives at one of two levels. An instance variable belongs to each object, so each object gets its own copy. A class variable belongs to the class itself, so there is only one copy shared by all objects.
You mark a class variable with the static keyword before its type. The moment you add static, you are saying "this belongs to the class, not to any one object."
</>Javapublic class Student { // Instance variable - each Student object has its own name private String name; // Class variable - one shared copy for all Student objects private static int totalStudents = 0; public Student(String studentName) { this.name = studentName; totalStudents++; // every new Student updates the shared count } public String getName() { return name; } // Class method - reads the shared class variable public static int getTotalStudents() { return totalStudents; } }
Each Student keeps a separate name, but they all share one totalStudents. When any constructor runs totalStudents++, the change is visible to every object.
Accessing Public Class Variables and Methods
Because class variables and class methods belong to the class, you reach public ones with the class name and the dot operator. You do not need an object.
</>JavaStudent alice = new Student("Alice"); Student bob = new Student("Bob"); // Class method called on the class name, not an object System.out.println(Student.getTotalStudents()); // 2
Both objects increased the same shared counter, so the class reports 2. This is the behavior you trace on multiple-choice questions: one static variable, many objects, a single shared value.
What Class Methods Can and Cannot Do
A class method runs at the class level, so it does not automatically have an object to work with. That leads to two rules you need for the exam:
- A class method can access or change class variables and call other class methods.
- A class method cannot access instance variables or call instance methods unless it is passed an instance of the class as a parameter.
</>Javapublic class Counter { private int count; // instance variable private static int objectsMade = 0; // class variable public Counter() { count = 0; objectsMade++; } // Class method that gets an object passed in, so it CAN use instance data public static int readCount(Counter c) { return c.count; // allowed because c is an instance parameter } // Class method using only class-level data public static int howManyMade() { return objectsMade; } }
howManyMade works at the class level with no object. readCount can touch the instance variable count only because it receives a Counter object named c. Without that parameter, a class method has no instance to reach into.
Constants with final
Declaring a variable final means its value cannot be modified after it is assigned. Combining static and final is the standard way to make a shared constant that every object can use but no one can change.
</>Javapublic class Circle { public static final double PI = 3.14159; private double radius; public Circle(double r) { radius = r; } public double area() { return PI * radius * radius; } }
Here PI is one shared value for the whole class, and final stops any code from reassigning it. You can read it from outside with Circle.PI.
Tracing Shared Static Values and Spotting Compile Errors
Code Tracing: When you see one static variable and several objects, track a single shared value, not one per object. Each time any object changes the class variable, the new value carries over to the next line for every object. A common multiple-choice setup creates a few objects, has each constructor increment a static counter, then prints the counter.
Free Response: In a class design problem, read the specification for anything shared across all objects, such as a running total or count of objects created. Declare that as a static variable and update it in the constructor or methods. Keep regular per-object data as instance variables. Access public class members with the class name and the dot operator when you call them from outside the class.
Common Trap: If a class method tries to use an instance variable or call an instance method directly, the code will not compile. On "why will this not compile" questions, check whether a static method is reaching for instance data without being given an object. The fix is to pass an instance of the class as a parameter, then use that parameter to reach the instance data.
Common Misconceptions
- Misconception: Each object gets its own copy of a static variable.Actually: No. There is exactly one copy of a class variable, shared by every object. Changing it through any object changes it for all.
- Misconception: You need to create an object to call a static method.Actually: You call public class methods with the class name and dot operator, like
ClassName.method(). No object required. - Misconception: A static method can use instance variables like any other method.Actually: It cannot, unless it receives an object as a parameter. Class methods have no automatic object to work with.
- Misconception: final and static mean the same thing.Actually: They are different.
staticmeans the variable belongs to the class and is shared.finalmeans the value cannot be changed after assignment. A constant often uses both together. - Misconception: Calling a static variable through an object is the intended way to use it.Actually: Since the variable belongs to the class, access it through the class name to make the class-level meaning clear.
Related AP Computer Science A Guides
Practice Questions for 3.7
1 of 10
Consider the following class declaration and code segment.
</>Javapublic class Mystery { private static int total = 0; private int value; public Mystery(int val) { value = val; total += value; } public void process() { int total = 5; total += value; Mystery.total += total; } public static int getTotal() { return total; } }
</>JavaMystery m1 = new Mystery(2); Mystery m2 = new Mystery(3); m1.process(); System.out.println(Mystery.getTotal());
What is printed as a result of executing the code segment?
A.
13
B.
7
C.
12
D.
9
Vocabulary
The following words are mentioned explicitly in the AP® course framework for this topic.Term | Definition |
|---|---|
class methods | Methods that are associated with a class rather than with instances of the class, and include the static keyword in their header. |
class variable | Variables that belong to the class itself rather than individual objects and can be accessed or modified by accessor and mutator methods. |
dot operator | The symbol (.) used in Java to access instance methods and properties of an object. |
final keyword | A Java keyword that, when applied to a variable, prevents its value from being modified after initialization. |
instance methods | Methods that belong to an object and are called on specific instances of a class using the dot operator. |
instance variable | A variable that belongs to an object and can be accessed throughout the class, as opposed to a local variable that is limited to a specific block of code. |
parameter | A variable declared in the header of a method or constructor that receives values passed to the method when it is called. |
public access modifier | A keyword that allows class variables to be accessed outside of the class using the class name and dot operator. |
static keyword | A Java keyword used to designate that a variable belongs to the class rather than to individual objects of the class. |
See Fiveable more often in Google
Mark Fiveable as a preferred source and Google is more likely to show you our guides in Top Stories, with a "preferred" badge in AI Mode and AI Overviews.