Final modifier in Java is used to indicate a method cannot be overridden in a derived class. When final modifier is declared it prevents the class from acting as a base class and stops the derived class. This process will prevent late binding which in turn stops polymorphism. Final modifiers will always precede the statement as shown in the example.
private Booln EmpDetails;
Static final Wk= 9;
protected static final int Sal= 5000;
public static void main(String[ ] arguments)
{
// This method’s body content
}
The statement bodies which are in bold are final modifiers in the above program. Unlike class, final modifier applied to instance will simply mean that the instance variable cannot be changed.
- Many: Java interview Q&A available here
How to use Final Modifier in Java?
Now it’s clear for you that if you create a final int variable and initialize a value, say 5000 now you cannot the change the value for this variable.
You can make a class as final and protect it from inheritance but you can’t extend the final class in Java or modify the methods of final class. Also no inner or anonymous classes can be created. There are three kinds of variables used in Java they are:
- Static final variable
- Non-static final variable
- Local final variable
Static and Non-static final variable are initialized during the declaration while Local final variable can be declared only inside a method or block. Until the arrival of Java 8, the local variable was prevented from using anonymous classes but in Java 8 the local variable can be initialized only once and cannot be modified after initialization.
When you want to declare a method as final, there will be some restrictions to do it. For ex: An abstract method cannot be declared as a final in Java. The following program will explain you about to make a new final method in Java:
Class Big
{
Public final void 1surn() {
system.out.println(“No girl can’t change their Surname till marriage””);
}
}
Class babyDaughter extends Big
{
Public final void get 1surn()
{
System.out.println (“I am expecting my New surname soon”);
}
}
When this program is compiled, an error will popup which says “Cannot override a final method from Big”.
When to Use Final modifier in Java?
The final modifier always create doubt in programmer’s mind and raises questions like when to declare a method as final in Java and when to declare a method static. The answer to both these questions is simple. If you want to prevent a method from creating any sub-classes or prevent from changing its definition, you can declare it as final. This process will prevent the class from overriding. Following these instructions might help you in deciding whether to make a method final or not:
- Methods called from a constructor must be declared as a final because sub-classes can override and alter the purpose of the class.
- Always make the methods used for performance critical as final because compilers inline or cache such methods.
- If the use of a particular is over then it can be classified as final to prevent overriding.
- Sensitive methods which have the capability to override the classes are to be declared final.
- If you are using the template design pattern then declare the template method as final.
Final modifier is mainly used as safety design to prevent overriding of classes and the increase the compiler performance. Finals are also used to as a tool for document the program because it can be easily noted which class will maintain that particular code. If the final modifier is declared in private final method it will prevent any type of modification in subclass itself because the inner classes will binded using static binding. Other than that private final won’t give any additional value.
It can be concluded that final modifier is used as a preventive method to secure the tested code from overriding. If a method called from a constructor is not declared as final, it will result in malicious overriding from sub classes.