01 / 20
S

Class Variables
& Methods

Understand how static members belong to the class itself, not individual objects: shared data, utility methods, and the power of the static keyword in Java.

AP Computer Science A

What Are Class Variables?

A class variable (also called a static variable) is a variable declared with the static keyword. It belongs to the class itself, not to any single object. Every instance of the class shares the same copy of that variable.

public class Dog { static int count = 0; // class variable String name; // instance variable public Dog(String name) { this.name = name; count++; } }

What Are Class Methods?

A class method (also called a static method) is a method declared with the static keyword. It can be called without creating an object of the class. It can only access other static members directly.

public class MathHelper { public static int square(int x) { return x * x; } } // Called without an object: int result = MathHelper.square(5); // 25

Why Does Static Matter?

Shared State
Track data across all instances: counters, configuration values, or shared resources.
Utility Methods
Provide helper functions that don't need object state, like Math.max() or Collections.sort().
Memory Efficiency
One copy in memory instead of duplicating the same value across hundreds of objects.

The Memory Model

Dog class static count = 2 static getCount() dog1 name = "Rex" ( no count copy ) dog2 name = "Max" ( no count copy ) Both point to the same static count Static (Class Level) One copy, shared by all objects Instance (Object Level) Each object gets its own copy Dog.count and dog1.count and dog2.count all refer to the SAME variable

The static Keyword Rule

1
static variables are shared across all instances of the class. They belong to the class, not any object.
2
static methods can be called using the class name. They cannot access instance variables or call instance methods directly.
3
Instance methods can access both static and instance members. Static methods can only access static members.

Access Rules at a Glance

Static Members static variables static methods ClassName.member Instance Members instance variables instance methods objectName.member ✓ ✕ Instance → Static: ✓ Static → Instance: ✕ Static methods cannot use "this" keyword (because "this" refers to a specific object)

Static vs Instance

Static (Class) Instance (Object)
Declared with static keyword No static keyword
Belongs to The class itself Each individual object
Copies in memory One (shared) One per object
Accessed via ClassName.member objectName.member
Can use "this"? No Yes
Example Math.PI dog.name

When to Use Static

1
Ask: Does this data need to be shared across all objects? If yes, make it static.
2
Ask: Does this method need access to instance variables? If no, it can be static.
3
Constants that never change: use static final (e.g., Math.PI).
4
Counters and trackers: counting how many objects have been created is a classic use of static variables.
5
Utility / helper methods: pure functions that compute a result from their parameters only.

Example 1: Object Counter

public class Student { private static int totalStudents = 0; private String name; public Student(String name) { this.name = name; totalStudents++; } public static int getTotalStudents() { return totalStudents; } }

One ticket office counter, many fixed ticket IDs

One ticket agent hands a new stub to a patron while another patron retains an issued stub.Each issued ticket keeps its own number while one class counter advances. Shared class state and separate instance state serve different purposes.

Copy the number, then advance the counter

Two ticket instances retain IDs 1 and 2 while one static nextNumber advances to 3.The Ticket constructor copies nextNumber into the new instance and then increments the shared counter. Tickets keep IDs 1 and 2 while nextNumber becomes 3. This counter records constructions.

Example 2: Utility Class

public class StringUtils { public static boolean isPalindrome(String s) { String reversed = new StringBuilder(s) .reverse().toString(); return s.equals(reversed); } } // No object needed: boolean result = StringUtils.isPalindrome("racecar"); // true

Example 3: Static Final Constants

public class Circle { public static final double PI = 3.14159265; private double radius; public Circle(double radius) { this.radius = radius; } public double getArea() { return PI * radius * radius; } public static double computeArea(double r) { return PI * r * r; } }

Calling Static Members

// Preferred: use the class name int total = Student.getTotalStudents(); double area = Circle.computeArea(5.0); double pi = Circle.PI; // Legal but discouraged: using an object reference Student s = new Student("Alice"); int total2 = s.getTotalStudents(); // works, but misleading

Scope and Lifetime

Static Variable
Created when the class is first loaded. Exists for the entire program duration. Destroyed when the program ends.
Instance Variable
Created when "new" is called. Exists as long as the object is referenced. Destroyed by garbage collection.
Local Variable
Created when the method runs. Exists only during that method call. Destroyed when the method returns.

Common Mistakes

✕ Accessing Instance from Static
Trying to use an instance variable inside a static method. Java gives a compile error: "non-static variable cannot be referenced from a static context."
✕ Using "this" in Static
Writing "this.name" inside a static method. The "this" keyword refers to a specific object, and static methods have no object.
✕ Calling via Object Reference
Writing "myObj.staticMethod()" instead of "ClassName.staticMethod()". It compiles but is misleading and marked wrong on AP exams.

Question 1 of 5

Which keyword makes a variable or method belong to the class rather than to individual objects?

Question 2 of 5

What is printed?

Question 3 of 5

A static method cannot directly access which of the following?

Question 4 of 5

Which line of code will cause a compile error?

Question 5 of 5

Which of the following is the best reason to declare a variable as static final?