Language
Version: SE 8
Categories
-
User Program Communication
-
Variables
-
Control Flow
-
Object Oriented Programming
-
String Class
Object Oriented Programming
Class Variables in Java
Class variables are tied to the class itself, and its value is shared across all class objects.
Syntax
//within a class
modifier static variableName;
//accessing the class variable
className.variableName;
Notes
The static modifier prefix is what makes a variable a class variable.
Class variables can be accessed using the object, but this is discouraged.
Example
public class Car {
public static int numberOfWheels = 4; //all cars have 4 wheels
}
System.out.println("Number of wheels: " + Car.numberOfWheels);