Java - Class Variable (Static Fields)

> Procedural Languages > Java

1 - About

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.

Advertising

3 - Example

3.1 - 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.

3.2 - 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 (_).

lang/java/class_variable.txt · Last modified: 2017/02/12 21:05 by gerardnico