The final variable, method, and classes
We have mentioned a final
property several times in relation to the notion of a constant in Java, but that is only one case of using the final
keyword. It can be applied to any variable in general. Also, a similar constraint can be applied to a method and even a class too, thus preventing the method from being overridden and the class from being extended.
The final variable
The final
keyword placed in front of a variable declaration makes this variable immutable after the initialization, such as the following:
final String s = "abc";
The initialization can even be delayed:
final String s;
s = "abc";
In the case of an object
property, this delay can last only until the object is created. This means that the property can be initialized in the constructor, such as the following:
class A {
private final String s1 = "abc";
private final String...