CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Final Class in Java: Enforcing Immutability and Security

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Final Class in Java: Enforcing Immutability and Security

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X

A final class in Java is a class that cannot be extended (inherited). By declaring a class as final, you prevent other classes from subclassing it, ensuring its behavior remains unchanged. In this guide, you’ll learn how to use final classes, their benefits, and practical examples to secure your code.

Table of Contents

  • What is a Final Class in Java?
  • Why Use a Final Class?
  • Example: Declaring and Using a Final Class
    • 1. Define a Final Class
    • 2. Attempting to Extend (Error)
  • Final Class vs. Final Method vs. Final Variable
  • Best Practices
  • Conclusion
  • FAQs
    • Can a final class be abstract?
    • Can a final class have methods?
    • Is the String class really final?
    • Can a final class implement an interface?

What is a Final Class in Java?

A final class is declared using the final keyword:

public final class String { // java.lang.String is a final class  
    // Class implementation  
}  

Key Characteristics:

  • Cannot be subclassed: Any attempt to extend it causes a compile-time error.
  • Methods are implicitly final: Cannot be overridden (since the class itself can’t be extended).

Why Use a Final Class?

  1. Security: Prevent malicious or accidental subclassing (e.g., overriding methods to alter behavior).
  2. Immutability: Ensure critical classes like String remain unmodifiable.
  3. API Design: Lock down class behavior in libraries/frameworks.
  4. Performance: Some JVM optimizations apply to final classes.

Example: Declaring and Using a Final Class

1. Define a Final Class

public final class Logger {  
    public void log(String message) {  
        System.out.println("[LOG] " + message);  
    }  
}  

2. Attempting to Extend (Error)

// Compiler Error: Cannot inherit from final Logger  
class CustomLogger extends Logger {  
    // ❌ Not allowed!  
}  

Final Class vs. Final Method vs. Final Variable

KeywordApplies ToEffect
Final ClassClass declarationPrevents inheritance.
Final MethodMethod in a classPrevents overriding in subclasses.
Final VariableVariable (field/local)Makes the variable a constant (unmodifiable).

Best Practices

  1. Use Sparingly: Finalize only classes critical to security/immutability.
  2. Combine with Private Constructors: For utility classes (prevent instantiation).
  3. Document Reasons: Clarify why a class is final (e.g., “Security: prevent tampering”).
  4. Consider Alternatives: Use encapsulation or interfaces if extensibility is needed.

Conclusion

Final class in Java are essential for designing secure, immutable, and stable APIs. By preventing inheritance, they safeguard critical logic from unintended modifications. Use them judiciously to balance flexibility and control.

FAQs

Can a final class be abstract?

No. final and abstract are contradictory (abstract classes require subclassing).

Can a final class have methods?

Yes, but they can’t be overridden since the class can’t be extended.

Is the String class really final?

Yes. java.lang.String is final to ensure immutability and security.

Can a final class implement an interface?

Yes. Implementing an interface doesn’t require inheritance.

Related Posts:

  • Abstract Class in Java: Bridging Code Reusability…
  • Interface in Java: Mastering Abstraction and…
  • Abstraction in Java
  • Most Frequently Asked Spring Boot Interview…
  • Control Statements in Java
  • Inheritance in Java: A Developer’s Guide to Code Reusability
Tags:javalanguage-fundamentals
Was this article helpful?
← Previous ArticleAsymptotic Notation
Next Article →Working with Files and Directories in Java

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap