Final Variables and their Rules

Java 8 min min read Updated: Mar 31, 2026 Beginner
Final Variables and their Rules
Beginner Topic 18 of 25

Final Variables and Their Rules

In Java, the keyword final is used to restrict modification. When applied to variables, it ensures that the value assigned to the variable cannot be changed once it has been initialized.

The final keyword is very important for writing secure, predictable, and immutable code. It is widely used in constants, configurations, and object design.

Key Concept: A final variable is a variable whose value cannot be changed once it is assigned.

What is a Final Variable?

A final variable is a variable declared with the final keyword. Once a value is assigned to it, it cannot be reassigned.

Example

java final int x = 10; // x = 20; // Error: cannot reassign

Here, x is a final variable, so its value cannot be changed after initialization.

Why Use Final Variables?

  • to create constants
  • to prevent accidental modification
  • to improve code readability
  • to ensure immutability
  • to make code thread-safe (in some cases)

Types of Final Variables

Based on where and how they are declared, final variables can be categorized into:

  • Local final variables
  • Instance final variables
  • Static final variables

1. Local Final Variable

A final variable declared inside a method or block is called a local final variable.

Example

java public class Demo { public static void main(String[] args) { final int x = 100; System.out.println(x); // x = 200; // Error } }

Local final variables must be initialized before use and cannot be reassigned.

2. Instance Final Variable

A final variable declared inside a class but outside methods is called an instance final variable.

It must be initialized either:

  • at the time of declaration, or
  • inside a constructor

Example: Initialization at Declaration

java class Student { final int id = 101; }

Example: Initialization in Constructor

java class Student { final int id; Student(int id) { this.id = id; } }

Once assigned, the value cannot be changed.

3. Static Final Variable

A static final variable is a constant that is shared across all objects and belongs to the class.

It is usually written in uppercase by convention.

Example

java class Constants { static final double PI = 3.14159; }

Static final variables are commonly used for defining constants.

Initialization Rules for Final Variables

Final variables must be assigned a value exactly once.

  • Local final variables → must be initialized before use
  • Instance final variables → initialized at declaration or in constructor
  • Static final variables → initialized at declaration or in static block

Static Final Variable with Static Block

Static final variables can also be initialized inside a static block.

java class Demo { static final int x; static { x = 50; } }

Once initialized, x cannot be changed.

Blank Final Variable

A final variable that is declared but not initialized immediately is called a blank final variable.

It must be initialized before use.

Example

java class Demo { final int x; Demo(int value) { x = value; } }

Here, x is assigned in the constructor.

Final Reference Variable

When a reference variable is declared as final, the reference cannot be changed, but the object's internal state can still be modified.

Example

java class Student { int age; } public class Main { public static void main(String[] args) { final Student s = new Student(); s.age = 20; // Allowed // s = new Student(); // Error } }

Here:

  • The reference s cannot point to another object
  • But the object's data can still be modified

Final vs Const (C/C++ Comparison)

Java does not have a const keyword like C/C++. Instead, final is used to achieve similar functionality.

Feature Java (final) C/C++ (const)
Keyword final const
Reassignment allowed? No No
Used for constants Yes Yes

Final with Methods and Classes (Brief)

Although this topic focuses on variables, it is useful to know that final can also be used with methods and classes:

  • final method → cannot be overridden
  • final class → cannot be extended

Example:

java final class Demo { }

Execution Flow of Final Variables

Final variables follow strict initialization rules:

  • They receive memory during declaration
  • They must be assigned exactly once
  • After assignment, they cannot be modified

Example:

java class Demo { final int x = 10; Demo() { // x = 20; // Error } }

Real-World Example

Suppose you are developing an application where the maximum number of users should never change.

java class AppConfig { static final int MAX_USERS = 1000; }

This ensures that MAX_USERS remains constant throughout the application.

Common Mistakes

  • Trying to reassign a final variable
  • Forgetting to initialize a blank final variable
  • Confusing final reference with object immutability
  • Using final incorrectly with instance variables

Best Practices

  • Use static final for constants
  • Use uppercase naming for constants
  • Initialize final variables immediately when possible
  • Use final to make code more predictable
  • Avoid unnecessary use of final in simple cases

Interview-Oriented Points

  • Final variable cannot be reassigned after initialization
  • Final variables must be initialized exactly once
  • Static final variables are used as constants
  • Final reference cannot change, but object data can
  • Blank final variables must be initialized in constructor
  • Final improves immutability and code safety

Conclusion

Final variables are a key concept in Java that help enforce immutability and protect values from accidental changes. They are widely used for constants and configuration values in real-world applications.

Understanding how final variables work, how they are initialized, and how they behave with references is essential for writing clean, safe, and maintainable Java code.

Quick Summary: Final variables can be assigned only once and cannot be changed afterward. They are used for constants, immutability, and safe programming practices.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators