CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > final Keyword in Java

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 Keyword in Java

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 ยท 4 min read ยท 0 Comments
Share: in X

The final keyword in Java is a non-access modifier that can be used with variables, methods, and classes. It prevents the value of a variable, the behavior of a method, or the structure of a class from being changed.

Table of Contents

Use case of final keyword in Java
Why use final keyword in Java?
The final keyword and constants
The final keyword and methods
The final keyword and classes
Summary of the key points
FAQs

Use case of final keyword in Java

The final keyword can be used in Java in three ways:

  • To declare constants. A final variable can only be assigned once, and its value cannot be changed after that. This makes it useful for declaring constants, such as PI or the number of seconds in a minute.
  • To prevent methods from being overridden. A final method cannot be overridden by a subclass. This is useful for methods that are part of a class’s public API and should not be modified by subclasses.
  • To prevent classes from being inherited from. A final class cannot be inherited from by a subclass. This is useful for classes that should not be extended, such as the String class.

Why use final keyword in Java?

There are several reasons why we might want to use the final keyword in Java:

  • To improve code readability and maintainability. The final keyword can make our code more readable and maintainable by making it clear which variables and methods cannot be changed.
  • To prevent errors. The final keyword can help to prevent errors by preventing variables and methods from being changed in ways that could cause problems.
  • To enforce design decisions. The final keyword can be used to enforce design decisions, such as ensuring that a class cannot be extended or that a method cannot be overridden.

The final keyword and constants

To declare a final variable, we simply add the final keyword to the variable declaration.

For example:

final int MAX_VALUE = 100;

This declares a final variable named MAX_VALUE that is initialized to the value 100 and it cannot be changed after it is initialized.

A constant is a value that is fixed. We can create constants in Java using the final keyword. In the above example, the MAX_VALUE is also a constant.

The final keyword and methods

To declare a final method, we add the final keyword to the method declaration.

For example:

final void printHello() {
  System.out.println("Hello!");
}

This declares a final method named printHello() that prints the message “Hello!”. The printHello() method cannot be overridden by a subclass. If we try to override this method the compiler will show an error.

The final keyword and classes

To declare a final class, we add the final keyword to the class declaration.

For example:

final class Point {
  int x;
  int y;

  Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
}

This declares a final class named Point that has two fields, x and y. The Point class cannot be inherited from by a subclass.

When a class is declared as final, it cannot be subclassed. This means that no other class can inherit from the final class.

Summary of the key points

  • The final keyword in Java is a non-access modifier that can be used with variables, methods, and classes.
  • The final keyword can be used to declare constants, prevent methods from being overridden, and prevent classes from being inherited from.
  • There are several reasons why we might want to use the final keyword in Java, including improving code readability and maintainability, preventing errors, and enforcing design decisions.

FAQs

When should I use the final keyword for variables?

You should use final keyword in Java to apply restrictions on classes, methods, and variables. When applied to a class, it makes the class unable to be extended. When used with a method, it prevents the method from being overridden. And when used with a variable, it makes the variable a constant that cannot be reassigned after initialization.

Can a final method be overloaded in Java?

Yes, we can overload a final method in Java. Overloading means defining multiple methods in the same class with different parameter lists. The final keyword only prevents the method from being overridden in derived classes. But it doesn’t restrict us from defining multiple methods with the same name but different parameters in the same class.

Is it possible to change the value of a final variable in Java?

No, once a final variable is assigned a value, it cannot be changed or reassigned. Any attempt to modify the value of a final variable will result in a compilation error.

What’s the difference between final, finally, and finalize in Java?

final is a keyword used to make a class, method, or variable unchangeable or non-extendable.

finally is a keyword used in exception handling to specify a block of code that will always be executed whether an exception occurs or not.

finalize is a method in the Object class that can be overridden to perform cleanup operations on an object before it’s garbage collected.

These three keywords serve different purposes in Java and are not related to each other in functionality.

Related Posts:

  • Access Modifiers in Java
  • Constant in Java
  • Control Statements in Java
  • Variable in Java
  • Default Variable Initialization in Java
  • Packages in Java: A Guide to Modular, Maintainable Code
Tags:javalanguage-fundamentals
Was this article helpful?
โ† Previous ArticleLayout Management in Java Swing
Next Article โ†’FlowLayout in Java Swing

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