The final keyword in java is used to restrict the user. The java final keyword can be used in many context.
Final can be:
Final is a non-access modifier applicable only to a variable, a method or a class.
Basically final keyword is used to perform 3 tasks as follows –
- To create constant variables
- To prevent method overriding
- To prevent Inheritance
Final Variables –
When a variable is declared with final keyword, it’s value can’t be modified,essentially, a constant. This also mean that you must initialize a final variable. It is good practice to represent final variables in all uppercase, using underscore to separate words.
You can initialize a final variable when it is declared. A final variable is called blank final variable,if it is not initialized while declaration. Three ways in which you can initialize final variables –
- Initializing when you declare it.
- Initializing in constructor.
- Initializing in the instance initialization block (IIB)
Lets see a Java Program to see all the different ways in which we can initialize final variables –
//Java program to demonstrate different
// ways of initializing a final variable
// a final variable
// direct initialize
final int THRESHOLD = 5;
// a blank final variable
final int CAPACITY;
// another blank final variable
final int MINIMUM;
// a final static variable PI
// direct initialize
static final double PI = 3.14;
// a blank final static variable
static final double EULERCONSTANT;
// instance initializer block for
// initializing CAPACITY
CAPACITY = 25;
// static initializer block for
// initializing EULERCONSTANT
EULERCONSTANT = 2.3;
// constructor for initializing MINIMUM
// Note that if there are more than one
// constructor, you must initialize MINIMUM
// in them also
MINIMUM = -1;
Difference between Normal variable and Final variable –
The only difference between a normal variable and a final variable is that we can re-assign value to a normal variable but we cannot change the value of a final variable once assigned. Hence final variables must be used only for the values that we want to remain constant throughout the execution of program.
Final Methods –
When a method is declared with final keyword, it is called a final method. A final method cannot be overridden. Which means even though a sub class can call the final method of parent class without any issues but it cannot override it. We must declare methods with final keyword for which we required to follow the same implementation throughout all the derived classes.
Java Program Example of Final Method –
final void m1()
System.out.println("This is a final method.");
class B extends A
// COMPILE-ERROR! Can't override.
Final Classes –
When a class is declared with final keyword, it is called a final class. A final class cannot be extended(inherited). 2 reasons to make a class final –
- One is to prevent inheritance, as final classes cannot be extended.
- To create immutable class (like the predefined String class). You can not make a class immutable without making it final.
Java Program example of Final Class –
final class A
// methods and fields
// The following class is illegal.
class B extends A
// COMPILE-ERROR! Can't subclass A
Points to Remember:
1) A constructor cannot be declared as final.
2) Local final variable must be initializing during declaration.
3) All variables declared in an interface are by default final.
4) We cannot change the value of a final variable.
5) A final method cannot be overridden.
6) A final class not be inherited.
7) If method parameters are declared final then the value of these parameters cannot be changed.
8) It is a good practice to name final variable in all CAPS.
9) final, finally and finalize are three different terms. finally is used in exception handling and finalize is a method that is called by JVM during garbage collection.