Final Variable
A variable declared final can’t be modified. So final variables are by default read-only.
-
Final Variables improves performance as they can be cached.
-
Final variables are thread-safe.
Final Method
A method declared final can’t be overridden. Final methods are faster as they do not require to be resolved during runtime.
Final Class
Final Class is a class which can’t be sub-classed.
Eg: String.
Final Keyword helps to write Immutable Classes.
Few Points on final keyword. (From javarevisited)
-
Final keyword can be applied to member variable, method or class.
-
Final member variable must be initialized at the time of declaration or inside of constructor.
-
You cannot reassign a value to final keyword.
-
local final variable must be initialized during declaration.
-
Only final variable / Effective Final(java8) is accessible inside anonymous class.
-
Final methods can’t be overridden.
-
All variables declared inside interface are final.
-
Final Methods are bonded during compile time, called as static binding.
-
Making a class, method or variable final can improve performance as JVM can make assumptions and optimizations.
-
Making a collection variable final means only the reference can’t be changed, but the collection can be modified.
Eg:
private final List list = new ArrayList<>();
list.add("Item1"); // OK
list.add("Item2"); // OK
list = new ArrayList<>(); // ERROR.
What is immutable object in java? How can we write one?
Immutable classes are those classes whose object can not be modified once created, ie any alteration to immutable object will result in creation of new object. Example String.
Benefits,
- Provide Concurrency and multithreading advantages. Immutable objects are by default
thread safe
and can be shared without synchronization in concurrent environment. - Reusability : Immutable objects can be cached and reused.
Disadvantages,
- Expensive, if you frequently need to modify data as everytime a new object gets created.
- Reflection based frameworks are complicated by immutable objects since they requires constructor injection
To Write Immutable Object
- All the fields must be private and preferably final
- Don’t provide any setters for the fields ie
no mutators
. -
We need to make sure methods cannot be overridden
-
Strong Immutability : Make the Class final so cannot be overriden.
-
Weak Immutability : Make methods final and use static factories, keep constructors private. Eg If class A is weakly immutable, then a class B extending A will be partially immutable ie only behaviour defined by A will be immutable.
-
- Protect mutable fields
- Watch out for collections. Use Collections.unmodifiable. Also, collections should contain only immutable Objects.
- Don’t provide any methods that change the internal state of the Object Eg :
So All the getters must provide immutable objects or use defensive(deep) copying.
Template for creating immutable objects.
Make all fields private
Don't provide mutators
Ensure that methods can't be overridden by either making the class final (Strong Immutability) or making your methods final (Weak Immutability)
If a field isn't primitive or immutable, make a deep clone on the way in and the way out.