Topic 3.7 Notes – Class Variables and Methods
1. What Class Variables and Class Methods Are
You already know that a class is a blueprint for objects. Each object normally has its own copy of instance variables.
Here’s the big separation:
- Instance variable → each object gets its own copy
- Class variable (
static) → one shared copy for the entire class - Instance method → called on an object
- Class method (
static) → called on the class itself
Look at this example with a Student class:

Each Student object has its own name. But totalStudents exists only once and is shared by every object created from the class.
If something describes each object separately, it should be instance-level.
If it describes all objects together, it should be static.
2. Class Variables
A class variable:
- Is declared with
static - Belongs to the class
- Has exactly one shared copy
- Is shared across all objects
Example:
public class Student {
private String name; // instance
private static int count = 0; // class variable
public Student(String n) {
name = n;
count++;
}
}
Every time a Student is created, count increases. All students see the same value.
Accessing Static Variables
If a class variable is public, you access it with the class name:
Student.count
On the AP exam, they expect this format. Even though Java allows objectName.count, use the class name.
Static + Final (Constants)
When a variable is both static and final, it becomes a constant:
public static final int MAX_SCORE = 100;
finalmeans it cannot change after assignment.- Usually written in ALL_CAPS.
- Common on FRQs when you need a shared constant.
3. Class Methods
A class method:
- Is declared with
static - Belongs to the class
- Can be called without creating an object
Example:
public static int getCount() {
return count;
}
Called like:
Student.getCount();
What Static Methods CAN Access
✔️ Other static variables
✔️ Other static methods
public static int getCount() {
return count; // OK (static accessing static)
}
What Static Methods CANNOT Access
❌ Instance variables
❌ Instance methods
This will not compile:
public static void printName() {
System.out.println(name); // ERROR
}
Why? Because name belongs to a specific object. A static method doesn’t know which object you mean.
How to Use Instance Data in a Static Method
You must pass an object as a parameter:
public static void printName(Student s) {
System.out.println(s.name);
}
Now the method knows which student.
This exact idea shows up constantly in multiple choice questions where a static method tries to access an instance variable and causes a compile-time error.
4. When to Use Static vs Instance
Use static when:
- Tracking totals (number of objects created)
- Storing shared settings
- Declaring constants (
public static final) - Writing utility-style methods
Use instance members when:
- Data differs per object
- Behavior depends on object state
- The method modifies instance variables
If you’re unsure, ask:
Does this belong to each object separately, or to the class as a whole?
5. Common Testing Patterns
Here’s what your teacher and the AP love to do with this topic:
- Constructors that increment a static counter
- Multiple objects modifying the same static variable
- Static method trying to access instance data
- Determining whether code compiles
A classic trace pattern:
Student a = new Student("Ana");
Student b = new Student("Ben");
System.out.println(Student.getCount());
You must recognize that count increased twice.
Students often think each object has its own copy of a static variable. It does not. There is exactly one.