UNIT 3: CLASS CREATION · TOPIC 3.7
3.7 Class Variables and Methods
static members belong to the class, not to any single object. One copy, shared by everyone, accessible without creating an object.
What you need to know
- A class variable (static variable) is declared with
static:private static int count;There is one copy shared by all objects of the class. - Common uses: counting how many objects have been created, or constants:
public static final double RATE = 0.05; - A class method (static method) is declared with
staticand called on the class:ClassName.method(). It can be called without any object existing. - Static methods cannot access instance variables or call instance methods directly — there's no object to refer to. They can access static variables.
- Instance methods can access static variables and methods.
- Static variables are initialized when the class is first loaded, before any object is created. Their default values are the same as instance variables.
- Access a static variable from an instance method or from outside (if public) via the class name:
Student.count.
Worked example
public class Ticket
{
private static int nextId = 1; // shared by all tickets
public static final double FEE = 2.50; // constant
private int id; // each ticket has its own
public Ticket()
{
id = nextId;
nextId++;
}
public static int getNextId() { return nextId; } // OK: static uses static
public int getId() { return id; }
// public static int getId() { return id; } // ERROR: no object
}
Ticket a = new Ticket(); // id 1
Ticket b = new Ticket(); // id 2
System.out.println(Ticket.getNextId()); // 3
Exam tip: "Which variable should be static?" — anything that's the same for every object or tracks the class as a whole. "Why won't this compile?" — a static method touching an instance variable. Trace static variables as one shared cell, not one per object.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
static= belongs to the class. One copy for all objects, existing even if zero objects have been created. Non-static = belongs to each object.- Uses for static variables: counters across all instances (how many Tickets sold), shared configuration, and constants (
public static final double TAX = 0.07;). Constants are the most common on the exam. - Static methods can't use
this, can't access instance variables, and can't call instance methods without an object — because there may be no object. They can access static variables and call other static methods.mainis static, which is why it needs to create objects to call instance methods. - Instance methods can use static members freely. A
getPrice()instance method can read a staticTAXconstant. The restriction is one-directional. - Accessing static members:
ClassName.memberfrom anywhere it's visible.object.staticMembercompiles but is misleading and the exam avoids it. Inside the class, bare name. - Static initialization: static variables are set when the class loads, once. A static
count = 0then incremented in the constructor counts all objects ever created. - Tracing static state: keep one row for the static variable, separate from any object's rows. Every object shares that row.
Mistakes that cost points
- Static method touching an instance variable. Compile error. The single most common static question.
- Thinking each object has its own copy of a static variable. One copy, shared.
- Using
thisin a static method. Compile error. - Calling an instance method from main without an object. Compile error. Create an object first.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 A class
Player has private static int numPlayers = 0; incremented in its constructor. After creating three Player objects, what is numPlayers?Show answer
Answer: C. One shared variable, incremented three times.
Q2 Which of the following will not compile inside a class with
private int size; and private static int total;?Show answer
Answer: C. A static method can't access an instance variable — there's no object.
Key vocabulary
- Class variable / static variable
- a single variable shared by all objects of a class
- Class method / static method
- a method called on the class that can't use instance variables
- Constant
- a static final variable whose value never changes