Logout

final


The Java reserved word final means that the variable being defined cannot be changed.

So it is used for constant values.

It is customary for programmers to make the identifiers of constants to be ALL CAPS. But note that it is not the all caps that makes the variable unchangable, it is the fact that the Java reserved word final was used when the variable was defined.

Examples:


final int MAXRECORDS = 1300;

private final double PRICE = 3.4;
public static final int voltage = 97;

Often times also, you will see both static and final used together. This makes sense often, since the variable is a class variable, but one which cannot be changed. Math.pi is a good example of a static final variable. You don't need to make instances of Math to use it; there is only one pi. And, certainly, you would want to avoid the value for pi being changed, since it should indeed be constant.

So just to be clear, the following code would not be allowed:


public static final int voltage = 97;
while(notEnoughPower){
voltage++; //<---------ERROR
checkIfEnoughPower();
}