AP® Computer Science A glossary
Class variable
Also called: static variable
Course: AP® Computer Science A Shows up in: Unit 3: Class Creation
An attribute declared with the keyword static that belongs to the class, so all objects of the class share a single copy.
Appears in the course and exam description (CED): 3.1.A.33.7.B.13.7.B.2
Class variables are often counters or constants. Class methods and instance methods can both use them. A public class variable is reached from other classes with the class name and the dot operator.
public class Ticket
{
private static int sold = 0; // one copy for the class
public Ticket()
{
sold++;
}
public static void main(String[] args)
{
new Ticket();
new Ticket();
System.out.println(Ticket.sold);
}
}
Output:
2 Learn it properly, with a checkpoint 3.7 Class Variables and Methods