Java Programming Handbook

    Final Members in Java

    Introduction#

    In Java, the final keyword is used to impose restrictions on variables, methods, and classes. Once a member is declared final, it cannot be modified, overridden, or extended, depending on its usage. This blog will guide beginners through the concept of final members, explain their significance, and provide real-world examples.

    Topics Covered:#

    • What are Final Members in Java?
    • Final Variables
    • Final Methods
    • Final Classes
    • Differences Between Final, Static, and Constant Variables
    • When to Use Final Members?

    1. What are Final Members in Java?#

    The final keyword in Java is a modifier that can be applied to variables, methods, and classes. Its primary purpose is to prevent modifications and ensure stability in the code.

    Characteristics of Final Members:#

    • Final variables cannot be reassigned after initialization.
    • Final methods cannot be overridden in subclasses.
    • Final classes cannot be extended by other classes.
    • Helps maintain immutability and security in Java applications.

    2. Final Variables in Java#

    A final variable is a constant whose value cannot be changed after initialization. It can be initialized at the time of declaration or inside a constructor.

    Example of Final Variable:#

    class Configuration { final int MAX_USERS = 100; // Final Variable void displayLimit() { System.out.println("Maximum users allowed: " + MAX_USERS); } } public class Main { public static void main(String[] args) { Configuration config = new Configuration(); config.displayLimit(); // config.MAX_USERS = 200; // This will cause a compilation error } }

    Output:#

    Maximum users allowed: 100

    Example of Final Variable Initialized in Constructor:#

    class Person { final String name; // Constructor to initialize final variable Person(String name) { this.name = name; } void display() { System.out.println("Name: " + name); } } public class Main { public static void main(String[] args) { Person p = new Person("Alice"); p.display(); // p.name = "Bob"; // Compilation error } }

    3. Final Methods in Java#

    A final method prevents subclasses from overriding the method, ensuring that the original implementation remains unchanged.

    Example of a Final Method:#

    class Parent { final void showMessage() { System.out.println("This message cannot be overridden."); } } class Child extends Parent { // void showMessage() { // Compilation error: Cannot override final method // System.out.println("Trying to override final method"); // } } public class Main { public static void main(String[] args) { Child child = new Child(); child.showMessage(); } }

    Output:#

    This message cannot be overridden.

    4. Final Classes in Java#

    A final class cannot be extended by any other class, ensuring that its behavior remains unchanged.

    Example of a Final Class:#

    final class Utility { void displayMessage() { System.out.println("This class cannot be extended."); } } // class ExtendedUtility extends Utility { // Compilation error: Cannot inherit from final class // } public class Main { public static void main(String[] args) { Utility util = new Utility(); util.displayMessage(); } }

    Output:#

    This class cannot be extended.

    5. Differences Between Final, Static, and Constant Variables#

    FeatureFinal VariableStatic VariableConstant Variable (static final)
    Value ModificationCannot be modified after initializationCan be changed at any timeCannot be modified after initialization
    Memory StoragePer instance (unless static)Stored in class areaStored in class area
    AccessRequires an object (if non-static)Accessed using class nameAccessed using class name
    Examplefinal int x = 10;static int x = 10;static final int X = 10;

    6. When to Use Final Members?#

    Use CaseFinal VariableFinal MethodFinal Class
    Defining constants (e.g., PI, MAX_USERS)✅ Yes❌ No❌ No
    Preventing method overriding in subclasses❌ No✅ Yes❌ No
    Creating utility/helper classes that should not be extended❌ No❌ No✅ Yes
    Ensuring immutability (e.g., immutable objects)✅ Yes❌ No✅ Yes

    Conclusion#

    In this blog, we learned:

    • Final Variables: Prevent reassignment, useful for constants.
    • Final Methods: Prevent method overriding, useful for securing implementation.
    • Final Classes: Prevent inheritance, useful for utility and immutable classes.
    • Key differences between final, static, and constant variables.
    • Practical use cases of the final keyword in Java.

    Understanding final Members in Java is essential for writing secure, maintainable, and efficient code. It helps enforce immutability, prevent accidental changes, and improve the overall structure of applications.

    Last updated on Apr 09, 2025