final Keyword in Java
×


final Keyword in Java

124

Understanding the final Keyword in Java

The final keyword in Java is a modifier that can be used with variables, methods, and classes. It serves different purposes depending on where it is applied, such as creating constants, preventing method overriding, or stopping class inheritance. It’s a core concept in Java’s object-oriented nature that provides safety and clarity in code.

Using final with Variables

When a variable is declared as final, its value cannot be changed once assigned. This makes the variable a constant.

final int MAX_USERS = 100;
// MAX_USERS = 200; // Error: Cannot assign a value to final variable

It's good practice to declare constant values as final to avoid accidental modifications and make the code easier to understand.

Using final with Methods

If a method is marked final, it cannot be overridden by subclasses. This is useful when you want to lock down certain behavior in your class.

class Vehicle {
    final void startEngine() {
        System.out.println("Engine started");
    }
}

class Car extends Vehicle {
    // void startEngine() { ... } // Error: Cannot override the final method
}

Using final with Classes

When a class is declared as final, it cannot be extended. This is helpful when you want to prevent your class from being subclassed, for security or design reasons.

final class Utility {
    static void printInfo() {
        System.out.println("This is a utility class.");
    }
}

// class ExtendedUtility extends Utility { } // Error: Cannot inherit from final class

Key Differences Based on Usage

UsageEffect of final
VariableValue cannot be changed after assignment
MethodMethod cannot be overridden in subclasses
ClassClass cannot be extended/inherited


Important Notes on final Keyword

  • final variables must be initialized once, either at declaration or in a constructor.
  • A final reference variable cannot be re-pointed, but the object it refers to can still be modified.
  • Using final helps in writing immutable classes and ensuring critical methods aren't altered.

Example: final with Reference Variables

final StringBuilder builder = new StringBuilder("Hello");
builder.append(" World"); // Allowed
// builder = new StringBuilder(); // Error: Cannot assign a new object to final reference

Conclusion

The final keyword in Java is more than just a modifier—it’s a safeguard. Whether you're creating constants, sealing off methods, or locking classes from inheritance, final ensures that your design intentions are preserved and respected throughout your code.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat