Final Keyword
The common characteristic of the final keyword is that attaching it means restricting something
Final Class
Other classes
cannot inheritfrom itThe String class is also final!
Attempting to inherit the String class will cause an error
Final Method
Other methods
cannot overrideitThe method can no longer be modified
It prevents the method from being modified while extending to sub-classes
It is a method that cannot be redefined in child classes, preventing child classes from changing the behavior of the parent class's method
This helps maintain program consistency and prevent bugs!
Final Variable
It becomes a
constant valuethat does not change and cannot be reassignedSince it cannot be modified, an initial value is mandatory
However, if it is a variable inside an object, initialization through a
constructororstatic blockis allowed
The scope of the unmodifiable value is limited to the
value of that variableThat is, when referencing another object, the internal values of the referenced object can be changed
public static finalIt is a class constant that does not require an instance
It can be used once the class is loaded into memory
It is used in the form of
class.constant
Likewise, it must be initialized at the time of declaration
Last updated