Static Attributes
Estimated time to read: 1 minute
Overview¶
Attributes, including method attributes, can be set up so that they have only one value across all instances in the the same system. These variables are created with the keyword static
.
static int maxSpeed = 65;
If one Car
object changes the value of maxSpeed, all other Car
objects will also take on the new value.
This essentially creates a variable that can be shared by all objects of the same type.
Declaring Constants / Finals¶
Declaring an attribute to be final
mans that its value cannot be changed once assigned a value.
The example below creates a publicly accessible, static / shared across the class, constant / final, integer named MAX_SPEED with the value of 65.
public static final int MAX_SPEED = 65;
Note
constant / final is written in UPPER_CASE.