dev only

Class (static) Variables & Methods

Class Methods Belong to the Class, Not an Object

A static method or variable belongs to the class itself — there's exactly one shared copy, no matter how many objects (or zero objects) exist. Math.random() is the example you already know: you never make a new Math() first.

public class Subsystem
{
    public static int instanceCount = 0; // one shared copy for ALL Subsystem objects

    public Subsystem()
    {
        instanceCount++; // every constructor call increments the ONE shared counter
    }
}

Static Code Can't Touch Instance Data Directly

A static method can access other static members freely, but it cannot reach an instance variable or call an instance method — because static code runs without any particular object in hand, and instance data only exists per-object.

public class Subsystem
{
    private double currentSpeed; // instance variable — belongs to ONE object

    public static void printStatic()
    {
        System.out.println(currentSpeed); // compile error! no object to read this from
    }
}

The fix, if a static method genuinely needs instance data, is to pass in an actual object as a parameter:

public static void printSpeed(Subsystem s)
{
    System.out.println(s.currentSpeed); // fine — reading a specific object's data
}

Class Variables: One Shared Copy

A static variable is shared by every object of the class — changing it from one object's constructor or method affects what every other object sees, since there's only ever one copy.

public class Subsystem
{
    public static double maxObservedCurrent = 0;

    public Subsystem(double current)
    {
        if (current > maxObservedCurrent)
        {
            maxObservedCurrent = current; // updates the ONE shared value
        }
    }
}

This is a genuinely common real use: tracking a running maximum, minimum, or count across every object ever created from a class.

final for Constants

final makes a variable's value unchangeable after it's set — used for true constants, conventionally named in all caps:

public static final double MAX_SPEED = 1.0;

Trying to reassign a final variable later is a compile error.

Common Pitfalls

  • Trying to read an instance variable from a static method. It doesn't compile — static code has no specific object to read from, unless one is explicitly passed in as a parameter.
  • Expecting a static variable to have a separate copy per object. It doesn't — there's exactly one, shared by everything, which is precisely what makes it useful for things like a running count or max.

Key Takeaways

  • static members belong to the class itself — one shared copy, independent of how many objects exist.
  • Static code cannot access instance variables or instance methods directly — it needs an actual object passed in to do that.
  • A static variable's value is shared across every object of the class — useful for running totals, counts, or maximums.
  • final marks a variable as an unchangeable constant, conventionally named in ALL_CAPS.

Derived from csawesome-2026: unit-3-class-creation/3.07-class-static-variables-and-methods.md (CSA 3.7)