final variable in Java
A variable can be declared as final which prevents its contents from being modified. A final variable can be initialized only once but it can be done in two ways.
- Value is assigned when the variable is declared.
- Value is assigned with in a constructor.
Final variable exampleCompilation error if value of a final variable is changed in the setter method.
Final blank variable exampleCompilation error that the blank final variable is not initialized. In that case this variable should be initialized in a constructor of the class.
Final objectMaking an object reference variable final is a little different from a normal variable as in that case object fields can still be changed but the reference can't be changed. That is applicable to collections too, a collection declared as final means only its reference can't be changed values can still be added, deleted or modified in that collection.
Example of final object
final method parameterparameters of a method can also be declared as final to make sure that parameter value is not changed in the method. Since java is pass by value so changing the parameter value won't affect the original value anyway. But making the parameter final ensures that the passed parameter value is not changed in the method and it is just used in the business logic the way it needs to be used.
Final variables and thread safetyVariables declared as final are essentially read-only thus thread safe.
Also if any field is final it is ensured by the JVM that other threads can't access the partially constructed object, thus making sure that final fields always have correct values when the object is available to other threads.
final with inheritanceThe use of final with the method or class apply to the concept of inheritance.
final methodA method can be declared as final in order to avoid method overriding. Method declared as final in super class cannot be overridden in subclass. If creator of the class is sure that the functionality provided in a method is complete and should be used as is by the sub classes then the method should be declared as final.
Benefit of final methodMethods declared as final may provide performance enhancement too. Generally in Java, calls to method are resolved at run time which is known as late binding.
Where as in case of final methods since compiler knows these methods can not be overridden, call to these methods can be resolved at compile time. This is known as early binding. Because of this early binding compiler can inline the methods declared as final thus avoiding the overhead of method call.
Final method example
Final ClassA class declared as final can't be extended thus avoiding inheritance altogether.
If creator of the class is sure that the class has all the required functionality and should be used as it is with out extending it then it should be declared as final.
Declaring a class as final implicitly means that all the methods of the class are final too as the class can't be extended.
It is illegal to declare a class as both final and abstract. Since abstract class by design relies on subclass to provide complete implementation.
Example of final class
Benefits of final classSince all the methods in a final class are implicitly final thus final class may provide the same performance optimization benefit as final methods.
Restricting extensibility may also be required in some cases essentially when using third party tools.
A class may be declared final to enforce immutability.
Points to note -
- final keyword can be used with fields, methods and classes.
- final variables are essentially read only and it is a common convention to write the final variables in
all caps. As exp.
Private final int MAXIMUM_VALUE = 10
- final variables that are not initialized during declaration are called blank final variable in that case it forces the constructor to initialize it. Failure to do so will result in compile time error.
- final variables can be initialized only once, assigning any value there after will result in compile time error.
- final variables are read-only thus thread safe.
- In case of Final object reference can't be changed.
- final methods can't be overridden
- final classes can't be extended.
- final methods are bound during compile time (early binding), which means compiler may inline those methods resulting in performance optimization.
That's all for this topic final in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related TopicsYou may also like -
>>>Go to Java Basics page