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.

Why This Matters for the AP Computer Science A Exam
Class variables and methods show up when a problem needs data or behavior that is shared across all objects, like a counter that tracks how many objects have been created. On the multiple-choice section, you may need to trace code and predict output when several objects share one static variable, or spot why a class method cannot reach an instance variable. In free-response code writing, a class design problem may ask you to keep a shared count or define a behavior that works at the class level, so you need to declare and use static members correctly.
Knowing the line between class level and instance level also helps you avoid compile errors, which the exam tests when it asks why a code segment will not work and how to fix it.
Key Takeaways
- 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
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.
How to Use This on the AP Computer Science A Exam
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
- "Each object gets its own copy of a static variable." No. There is exactly one copy of a class variable, shared by every object. Changing it through any object changes it for all.
- "You need to create an object to call a static method." You call public class methods with the class name and dot operator, like
ClassName.method(). No object required. - "A static method can use instance variables like any other method." It cannot, unless it receives an object as a parameter. Class methods have no automatic object to work with.
- "final and static mean the same thing." 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. - "Calling a static variable through an object is the intended way to use it." 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
Vocabulary
The following words are mentioned explicitly in the College Board Course and Exam Description 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. |
Frequently Asked Questions
What does static mean in AP Computer Science A?
Static means the variable or method belongs to the class, not to one object. There is one shared class-level member instead of a separate copy for each instance.
What is a static variable in Java?
A static variable is a class variable shared by all objects of the class. It is declared with the static keyword before the variable type.
What is a static method in Java?
A static method is a class method that can be called with the class name and does not require an object. It can access class variables and call other class methods.
Can a static method access instance variables?
Not directly. A static method has no automatic object, so it can access instance variables only if an object is passed as a parameter.
How do you call a public static method?
Call it with the class name and dot operator, such as ClassName.methodName(). That makes clear the method belongs to the class.
What is the difference between static and final?
Static means a member belongs to the class and is shared. Final means a variable cannot be reassigned after it is set. Constants often use both static and final.