Final Keyword in Java

Final Keyword in Java

1. Introduction:

In Java, the final keyword is used to denote immutability or restrict further modification. It can be applied to variables, methods, and classes, and each use has its implications.

2. final Variables:

2.1. Introduction:

When a variable is declared with the final keyword, its value cannot be modified once assigned, essentially making it a constant.

2.2. Usage:

  • Local Variables:

    final int x = 10; 
  • Instance Variables:

    public class MyClass { final int MY_CONSTANT = 100; } 
  • Static Variables:

    public class MyClass { static final double PI = 3.14159; } 

Note: If a final variable is not initialized at the time of declaration, it's called a blank final variable. It must be initialized in the constructor.

3. final Methods:

3.1. Introduction:

When a method is declared as final, it cannot be overridden by subclasses.

3.2. Usage:

public class Parent { final void myFinalMethod() { System.out.println("This method cannot be overridden."); } } public class Child extends Parent { // Attempting to override myFinalMethod would result in a compile-time error } 

4. final Classes:

4.1. Introduction:

When a class is declared as final, it cannot be subclassed (extended).

4.2. Usage:

public final class MyFinalClass { // ... class contents ... } // The following would result in a compile-time error: public class AnotherClass extends MyFinalClass { // ... } 

5. Notes:

  • final vs. Immutability: Declaring an object reference as final only means that the reference itself cannot be changed, not the object's contents. For example, you can change the content of a final array or modify the attributes of a final object.

  • final with Inheritance: The final keyword prevents a subclass from changing a method implementation (by overriding) or extending a class.

6. Benefits of final:

  • Optimization: Sometimes, making a method final can assist the compiler in making optimization decisions.

  • Intention Clarity: By using final, developers make it clear that a variable's value shouldn't be changed, a method shouldn't be overridden, or a class shouldn't be extended.

  • Security: In some cases, classes are made final to ensure their behavior can't be changed by malicious subclasses.

7. Example:

public final class FinalExample { private final int id; private final String name; public FinalExample(int id, String name) { this.id = id; this.name = name; } public final void display() { System.out.println("ID: " + id + ", Name: " + name); } // Since the object's attributes are final and no setters are provided, // this object is effectively immutable. } public class Main { public static void main(String[] args) { FinalExample obj = new FinalExample(1, "Java"); obj.display(); } } 

8. Conclusion:

The final keyword in Java provides developers with a way to write safer code by creating immutable data, preventing method overriding, and blocking class extension. Using final correctly can ensure more secure and maintainable code.


More Tags

laravel-4 voice-recognition firebase-cloud-messaging sonata-admin jframe nav greatest-n-per-group ireport swagger-editor stata

More Programming Guides

Other Guides

More Programming Examples