PrepGo

Class Variables and Methods - AP Computer Science A Study Guide

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Learn with study guides reviewed by top AP teachers. This guide takes about 11 minutes to read.

Getting Started

So far, we have designed classes where every object has its own unique set of data. For example, two different Student objects would each have their own name and gpa. But what if we need a piece of data or a behavior that belongs to the class as a whole, rather than to any single object? This chapter introduces class-level variables and methods, which are shared across all instances of a class, allowing us to model concepts like object counters or utility functions.

What You Should Be Able to Do

  • Distinguish between variables and methods that belong to an object (instance members) and those that belong to a class (class members).

  • Declare class variables and methods using the static keyword in Java.

  • Call a class method using the name of the class, without needing to create an object.

  • Explain why a static method cannot access the instance variables or instance methods of its class.

  • Trace the value of a static variable as multiple objects of a class are created.

Key Concepts & Java Implementation

The Core Idea

In object-oriented programming, we typically associate data and behavior with individual objects. A variable that belongs to a specific object is called an instance variable. Each object created from a class gets its own copy of the instance variables. For example, if a Car class has an instance variable color, every Car object can have a different value for color.

However, some properties belong to the class itself. A class variable is a single variable that is shared among all objects of that class. There is only one copy of this variable, no matter how many objects are created. This is useful for things like keeping a count of how many objects have been instantiated or defining a constant value (like PI) that is relevant to the entire class.

Similarly, an instance method is a method that is called on a specific object and typically works with that object's instance variables. A class method, on the other hand, belongs to the class and is not associated with a particular object. These methods can be called directly using the class name. They are often used as "helper" or "utility" functions that perform a task related to the class but don't need to know the state of any specific object.

In Java, we use the static keyword to declare class variables and class methods.

Syntax & Implementation

The static keyword is a non-access modifier used to create class-level members.

  • Syntax Table
ElementPurposeJava Declaration Example
static variableA variable shared by all instances of a class. Only one copy exists.private static int studentCount = 0;
static methodA method that belongs to the class, not a specific object.public static int getStudentCount() { ... }
  • Annotated Java Examples

Example 1: Tracking Object Creation with a static Variable

Let's create a Gadget class that counts how many Gadget objects have been created.


public class Gadget {

    // Instance variable: each Gadget has its own serial number.

    private int serialNumber;


    // Class variable: ONE counter shared by ALL Gadget objects.

    private static int gadgetCount = 0;


    // A constructor is a special method called when a new object is created.

    public Gadget() {

        // Increment the shared counter each time a new Gadget is made.

        gadgetCount++;

        // Assign the current count as this object's unique serial number.

        this.serialNumber = gadgetCount;

    }


    // Instance method: returns the serial number for a specific Gadget.

    public int getSerialNumber() {

        return this.serialNumber;

    }


    // Class method: returns the total count of Gadgets created.

    public static int getGadgetCount() {

        // Note: This method CANNOT access 'this.serialNumber' because

        // it doesn't belong to any specific object.

        return gadgetCount;

    }

}

Example 2: Using a static Method as a Utility

Static methods are perfect for utility classes, where you just need to perform a calculation and don't need an object. Java's built-in Math class works this way.


public class GeometryUtils {

    // A public constant (static and final) available to anyone.

    public static final double PI = 3.14159;


    // A static method to calculate the area of a circle.

    // It doesn't need an object, just the radius to do its work.

    public static double circleArea(double radius) {

        return PI * radius * radius;

    }

}

Tracing & Analysis

  • Execution Trace

Let's trace the execution of code that uses our Gadget class and see how the static variable gadgetCount behaves.

Code:


System.out.println("Gadgets created so far: " + Gadget.getGadgetCount());


Gadget g1 = new Gadget();

System.out.println("Gadget 1 S/N: " + g1.getSerialNumber());


Gadget g2 = new Gadget();

System.out.println("Gadget 2 S/N: " + g2.getSerialNumber());


System.out.println("Gadgets created so far: " + Gadget.getGadgetCount());

Trace:

Line of CodeGadget.gadgetCountg1.serialNumberg2.serialNumberOutput
Gadget.getGadgetCount()0(n/a)(n/a)Gadgets created so far: 0
new Gadget()11(n/a)
g1.getSerialNumber()11(n/a)Gadget 1 S/N: 1
new Gadget()212
g2.getSerialNumber()212Gadget 2 S/N: 2
Gadget.getGadgetCount()212Gadgets created so far: 2
  • Analysis

Notice that gadgetCount is a single value that is incremented with each call to the Gadget constructor. It is shared across the entire class. In contrast, serialNumber is an instance variable; g1 and g2 each have their own separate copy.

A crucial rule is that static methods cannot access instance variables or instance methods directly. A static method is not called on a specific object, so it has no this reference to work with. If the getGadgetCount() method tried to access this.serialNumber, the compiler would produce an error because it wouldn't know which gadget's serial number to return.

Java Syntax Quick-Reference

A compact list of the new syntax introduced in this topic.

  • static: A Java keyword that declares a variable or method as belonging to the class rather than to a specific instance.

  • ClassName.staticMethodName(): The standard syntax for calling a static method. You invoke it on the class itself.

  • ClassName.staticVariableName: The standard syntax for accessing a public static variable.

Core Code Examples & Terminology

  • Instance Variable: A variable declared inside a class but outside any method, for which each object of the class has its own separate copy.

  • Class Variable: A variable declared with the static keyword. Only one copy of this variable exists, and it is shared among all instances of the class.

  • Instance Method: A method that belongs to an object. It can access both instance variables and static variables of the class.

  • Class Method: A method declared with the static keyword. It belongs to the class and can be called without creating an object. It can only access static variables and other static methods.

  • Core Snippet 1: Declaring a static variable

    
    // A counter shared by all objects of the class
    
    private static int instanceCounter = 0;
    

    This creates a single integer variable, instanceCounter, that is shared across all objects of its class.

  • Core Snippet 2: Accessing a static variable in a constructor

    
    public MyClass() {
    
        // Every time a new object is made, increment the shared counter.
    
        instanceCounter++;
    
    }
    

    This constructor modifies the single, shared static variable each time it is run.

  • Core Snippet 3: Declaring and calling a static method

    
    public class MathHelper {
    
        public static int add(int a, int b) {
    
            return a + b;
    
        }
    
    }
    
    // Calling the method from another class:
    
    int sum = MathHelper.add(5, 10); // sum is 15
    

    This shows a utility method that can be called directly on the MathHelper class without creating an object.

Core Skill Check

  • Code Tracing: What is the final value of count after this Java code runs:

    
    // In a class named 'Counter'
    
    public static int count = 0;
    
    public Counter() { count++; }
    
    
    
    // In main method
    
    Counter c1 = new Counter();
    
    Counter c2 = new Counter();
    
    Counter.count = Counter.count + 5;
    

    Answer: 7

  • Debugging: Identify the compile-time error in this Java code from the Player class:

    
    public class Player {
    
        private String name;
    
        private static int teamScore = 0;
    
        
    
        public static void printScore() {
    
            System.out.println("Score: " + teamScore + " for player " + this.name);
    
        }
    
    }
    

    Answer: The error is this.name. A static method like printScore cannot use the this keyword or access instance variables like name.

  • Application: Write a single line of Java code to call the public static method convertFahrenheitToCelsius from a class named TemperatureConverter, passing in the value 98.6.

    Answer: double celsius = TemperatureConverter.convertFahrenheitToCelsius(98.6);

Common Misconceptions & Errors

  1. Trying to access instance members from a static context. A static method cannot reference non-static variables or methods of its class because it is not associated with any particular object.

  2. Calling static methods on an object. While Java allows myObject.staticMethod(), it is misleading and bad practice. The call is resolved at compile time using the object's class type, not the object itself. Always use the class name: ClassName.staticMethod().

  3. Forgetting that static variables are shared. Modifying a static variable affects all objects of that class because there is only one copy. This is a feature, but can be a source of bugs if not handled intentionally.

  4. Confusing constants with static variables. A constant is a variable whose value cannot change, declared with final. It is common to make constants static (e.g., public static final double PI = 3.14;) so there is only one shared, unchangeable copy. However, a static variable does not have to be final; it can be changed, as seen with our counters.

Summary

Class variables and methods, declared with the static keyword, provide functionality that belongs to a class as a whole rather than to individual objects. A static variable is shared among all instances of a class, making it ideal for counters or shared constants. A static method can be called directly on the class (ClassName.methodName()) without creating an object, which is perfect for creating utility functions. The key limitation to remember is that static methods operate at the class level and therefore cannot access the instance-specific data of any object. Understanding this distinction between instance members and class members is fundamental to designing robust and logical object-oriented programs in Java.