Here we will discuss about final keyword in java. final can we be used with variable, method and class.
- final variable – if we define any variable as final it’s value can’t be changed.
- final method – if we declare method as final then we can’t override it.
- final class – if we define class as final we can not further extends it.
Let’s see one by one in details –
final variable –
Let’s see a simple example of how to define a final variable.
Example 1 –
public class FinalVariableExample1 { final int a = 10; public static void main(String[] args) { // we can't access directly variable a in static context, we need to // create object FinalVariableExample1 example1 = new FinalVariableExample1(); System.out.println(example1.a); } }
Output is – 10
Example 2 – Blank final variable.
Blank final variable we rarely use in real time project, but in interview you may be asked this question. Suppose we have only given declaration for final variable, In this case we need to initialize the final variable inside constructor.
public class FinalVariableExample2 { final int a;// only declaration FinalVariableExample2() { a = 10;// we need to initialize inside constructor. } public static void main(String[] args) { // we can't access directly variable a in static context, we need to // create object FinalVariableExample2 example2 = new FinalVariableExample2(); System.out.println(example2.a); } }
Output is – 10
Example 3 – Another example of Blank final variable where we will initialize the final variable in constructor only but we will pass the value through constructor.
public class FinalVariableExample2 { final int a; FinalVariableExample2(int number) { // value of number is 10 which will further assign to final variable a. a = number; } public static void main(String[] args) { // we can't access directly variable a in static context, we need to // create object FinalVariableExample2 example2 = new FinalVariableExample2(10); System.out.println(example2.a); } }
Output is – 10
Real time use of final variable – Let’s see how we use final variable in real time development.
One possible scenario to use final variable as constants. See an example as below –
public class YourApplicationConstants { public static final String PERMANENT = "PNT"; public static final String CONTRACT = "CNT"; // can be n numbers of constants. }
class Employee { String employeeType; public String getEmployeeType() { return employeeType; } public void setEmployeeType(String employeeType) { this.employeeType = employeeType; } } class ContractEmployee extends Employee { } class PermanentEmployee extends Employee { } public class StaticVariableExample { public static void main(final String[] args) { Employee e = new Employee(); // we are doing hard code. It should come from DB. e.setEmployeeType(YourApplicationConstants.CONTRACT); // this will always true just for understanding. if (e.getEmployeeType().equals(YourApplicationConstants.CONTRACT)) { System.out.println("salary should credit 3rd of every month"); } if (e.getEmployeeType().equals(YourApplicationConstants.PERMANENT)) { System.out.println("salary should credit 30th of every month"); } } }
Final Method in java –
We can’t override final method, it will give compilation error.
class Animal { public final void m1() { System.out.println("this is a final method, we can't ovverride"); } } class Tiger extends Animal { @Override public final void m1() { System.out.println("this is a final method, we can't ovverride"); } } public class FinalMethodExample { public static void main(String[] args) { System.out.println("test"); } }
Below compilation error will come –
Final class in java –
We can’t extends final class, it will give compilation error.
final class Aimal { } class Tiger extends Animal { } public class FinalClassExample { public static void main(String[] args) { System.out.println("test"); } }
the above program will give compilation error.