The final keyword in Java is a non-access modifier that can be applied to variables, methods, and classes. It is used to restrict the user from further modifying the entity to which it is applied. This keyword plays a crucial role in ensuring immutability and preventing inheritance or method overriding.
Usage
1. final Variables
A final variable is a constant; once initialized, its value cannot be changed.
A final variable must be initialized when it is declared or within a constructor if it is an instance variable.
publicclassFinalInitialization{
final int MAX_VALUE;publicFinalInitialization(){MAX_VALUE=100;}}제공
2. final Methods
A final method cannot be overridden by subclasses, ensuring that the method's implementation remains unchanged.
classParent{public final voiddisplay(){
System.out.println("This is a final method.");}}제공
3. final Classes
A final class cannot be subclassed, preventing inheritance.
public final classFinalClass{// Class implementation}제공
Examples
Example 1: final Variable
publicclassFinalVariableExample{publicstaticvoidmain(String[] args){
final int MAX_VALUE=100;
System.out.println("MAX_VALUE: "+MAX_VALUE);// MAX_VALUE = 200; // This will cause a compilation error}}제공
In this example, MAX_VALUE is a final variable. Attempting to reassign it will result in a compilation error.
Example 2: final Method
classParent{public final voiddisplay(){
System.out.println("This is a final method.");}}classChildextendsParent{// public void display() { // This will cause a compilation error// System.out.println("Attempting to override.");// }}publicclassFinalMethodExample{publicstaticvoidmain(String[] args){
Child obj =newChild();
obj.display();}}제공
Here, the display method in the Parent class is marked as final, preventing it from being overridden in the Child class.
Example 3: final Class
public final classFinalClass{publicvoidshow(){
System.out.println("This is a final class.");}}// class SubClass extends FinalClass { // This will cause a compilation error// }publicclassFinalClassExample{publicstaticvoidmain(String[] args){
FinalClass obj =newFinalClass();
obj.show();}}제공
In this example, FinalClass is declared as final, which means it cannot be extended by any other class.
Tips and Best Practices
Immutability: Use final variables to create constants and ensure that their values remain unchanged throughout the program.
Security: Use final methods to prevent subclasses from altering critical methods, which can help in maintaining security and consistency.
Design: Use final classes to prevent inheritance when designing immutable classes or when you want to ensure that the class's implementation cannot be altered.
Static Final Variables: Unlike instance final variables, static final variables cannot be assigned via constructors. They are typically initialized in their declaration.
publicclassFinalStaticExample{publicstatic final double PI=3.14159;}제공
Performance: In some cases, the use of final can help the Java compiler and JIT compiler to optimize the code, as it provides more information about the invariability of the value.
The
finalkeyword in Java is a non-access modifier that can be applied to variables, methods, and classes. It is used to restrict the user from further modifying the entity to which it is applied. This keyword plays a crucial role in ensuring immutability and preventing inheritance or method overriding.Usage
1.
finalVariablesA
finalvariable is a constant; once initialized, its value cannot be changed.Initialization of
finalVariablesA
finalvariable must be initialized when it is declared or within a constructor if it is an instance variable.2.
finalMethodsA
finalmethod cannot be overridden by subclasses, ensuring that the method's implementation remains unchanged.3.
finalClassesA
finalclass cannot be subclassed, preventing inheritance.Examples
Example 1:
finalVariableIn this example,
MAX_VALUEis afinalvariable. Attempting to reassign it will result in a compilation error.Example 2:
finalMethodHere, the
displaymethod in theParentclass is marked asfinal, preventing it from being overridden in theChildclass.Example 3:
finalClassIn this example,
FinalClassis declared asfinal, which means it cannot be extended by any other class.Tips and Best Practices
finalvariables to create constants and ensure that their values remain unchanged throughout the program.finalmethods to prevent subclasses from altering critical methods, which can help in maintaining security and consistency.finalclasses to prevent inheritance when designing immutable classes or when you want to ensure that the class's implementation cannot be altered.finalcan help the Java compiler and JIT compiler to optimize the code, as it provides more information about the invariability of the value.