Tuesday, 28 April 2015

final in Java

final keyword in java has its usage in preventing the user from modifying a field, method or class.
  • final field - A variable declared as final prevents the content of that variable being modified.
  • final method - A method declared as final prevents the user from overriding that method.
  • final class - A class declared as final can not be extended thus prevents inheritance.

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.
Please note that the final variable that has not been assigned a value while declaring a variable is called blank final variable, in that case it forces the constructor to initialize it.

Final variable example

final variable value can't be changed
Compilation error if value of a final variable is changed in the setter method.
final variable value can't be changed
Compilation error if value of a final variable is changed using an object.

Final blank variable example

final blank variable
Compilation error that the blank final variable is not initialized. In that case this variable should be initialized in a constructor of the class.

Final object

Making 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 parameter

parameters 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 safety

Variables 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 inheritance

The use of final with the method or class apply to the concept of inheritance.

final method

A 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 method

Methods 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 Class

A 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 class

Since 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 Topics

  1. finalize method in Java
  2. finally block
  3. final Vs finally Vs finalize
You may also like -
>>>Go to Java Basics page

No comments:

Post a Comment