Java - Class Variable (Static Fields)


A class variable is any field declared with the modifier static.

When the value of a static variable changes in any instance it affects the value for all instances.


Number of gears for a particular kind of bicycle

A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances.

constant named PI

For example, the following variable declaration defines a constant named PI, whose value is an approximation of pi (the ratio of the circumference of a circle to its diameter):

static final double PI = 3.141592653589793;

Constants defined in this way cannot be reassigned, and it is a compile-time error if your program tries to do so. By convention, the names of constant values are spelled in uppercase letters. If the name is composed of more than one word, the words are separated by an underscore (_).

Powered by ComboStrap