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
// (kept public here for brevity, so the examples below can read/write it directly —
// in real code a static field is usually private, with a static getter, same as an
// instance field, Ch.7.2)
public Subsystem()
{
instanceCount++; // every constructor call increments the ONE shared counter
}
}
Read or write a static member through the class name, not through an object — Subsystem.instanceCount, not someSubsystem.instanceCount. Accessing it through an object variable actually compiles (Java allows it), but it's misleading: it reads as if each object had its own instanceCount, when there's really only ever the one, shared copy.
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:
// inside Subsystem — private access to another Subsystem's field is allowed
// here because it's still the same class, just a different object (Ch.7.1)
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 for brevity here too — see the note above
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.
A common pattern: gather a class's constants into their own class, so other code reads them as Constants.MAX_SPEED instead of scattering public static final fields across whatever class happens to use them first:
public class Constants
{
public static final double MAX_SPEED = 1.0;
public static final int MAX_CURRENT_AMPS = 40;
}
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
staticmembers 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.
- Access a
staticmember through the class name (Subsystem.instanceCount), not through an object — an object reference compiles but is misleading. finalmarks a variable as an unchangeable constant, conventionally named in ALL_CAPS; a dedicatedConstantsclass is a common place to collect them.
Derived from csawesome-2026: unit-3-class-creation/3.07-class-static-variables-and-methods.md (CSA 3.7)