Java Reference
In-Depth Information
ables that are final must be initialized before they are used. This is typ-
ically done directly in the declaration:
final int id = nextID++;
You can defer the initialization of a final field or local variable. Such a
final variable is called a blank final. A blank final field must be initialized
within an initialization block or constructor (if it's an instance field) while
a blank final local variable, like any local variable, must be initialized be-
fore it is used.
Blank final fields are useful when the value of the field is determined by
a constructor argument:
class NamedObj {
final String name;
NamedObj(String name) {
this.name = name;
}
}
or when you must calculate the value in something more sophisticated
than an initializer expression:
static final int[] numbers = numberList();
static final int maxNumber; // max value in numbers
static {
int max = numbers[0];
for (int num : numbers) {
if (num > max)
max = num;
}
maxNumber = max;
}
 
Search WWH ::




Custom Search