With a simple example like yours, nothing bad can happen. However, it is possible for a final
field to be visible as uninitialised if you use questionable practices such as calling an overridable method in your constructor.
For example, the following program prints "My favourite colour is null", even though it references the final variable favouriteColour
, which is set to "blue"
in the constructor.
abstract class SuperClass {
final String favouriteColour;
SuperClass() {
announceFavouriteColour();
favouriteColour = "blue";
}
abstract void announceFavouriteColour();
}
public class FinalTest extends SuperClass {
void announceFavouriteColour() {
System.out.println("My favourite colour is " + favouriteColour);
}
public static void main(String[] args) {
new FinalTest();
}
}