Sitemap
Manisha Shukla

Manisha Shukla

I write on software testing( Manual testing ,Automation,API, Java etc.)

3 min readMar 16, 2025

--

Final Keyword in Java

Final is a non-access modifier applicable to a variable, method, and class.

  • Final Variable:- To create a constant variable
  • Final Method:- Prevent Method Overriding
  • Final Class:- Prevent Inheritance

Initialization of Final Variable

  • We must initialize the final variable otherwise the compiler will throw a compile time error.
  • Initialize a final variable when it is declared. A final variable is called a Blank Final variable, if it is not initialized while declaration.
  • A Blank Final variable can be initialized inside IIB or Constructor. If have more than one constructor in a class then it must be initialized in all of them otherwise compiler time error will be thrown.

Example:

class MyClass{

final int A = 5; //direct initialize

final int B; // blank final variable

final int C;

static final double D = 3.1453;

static final double E;

//IIB

{

B=25;

}

//SIB

static {

E=2.576;

}

//constructor

MyClass() {

C=2; //constructor to initialize the value of C, if there will be more than one constructor then have to initialize ‘C’ in all of them

}

}

  • The only difference between the normal variable and the final variable is that we can re-assign the normal variable but we cannot change the value of the final variable once assigned. Hence final variable must be used only for the value that we want to remain constant throughout the execution.
  • When a final variable is a reference to an object then this final variable is called the reference final variable.

Example:

class MyClass{

public static void main(String[] args){

final StringBuilder sb = new StringBuilder(“Hello”);

System.out.println(sb);

sb.append(“World”);

System.out.println(sb);

}

}

The final variable cannot be re-assigned. But in the above case of a reference final variable internal state of the object pointed by that reference variable can be changed. This is not re-assigned. This property of the final is called non-transitivity.

  • String Builder objects are like String objects except that they can be modified. Hence Java String Builder class is also used to create mutuable string objects.
  • The non-transitivity property also applies to arrays because arrays are objects in java. An array with final keywords is also called a final array.

Example:

Get Manisha Shukla’s stories in your inbox

Join Medium for free to get updates from this writer.

class MyClass{

static final int A = 5;

public static void main(String[] args){

A = 5; //Compile time error

}

}

When a final variable is created inside a method/constructor/block, it is called a local final variable and it must be initialized once it is created.

Example:

class MyClass{

public static void main(String[] args){

final int A;

A = 20;

}

}

Example:

class MyClass{

public static void main(String[] args){

int[] arr = {1,2,3};

for(final int i : arr){

System.out.println(i);

}

}

}

Since the variable ‘i’ goes out of scope with each iteration of the loop. It is actually a re-declaration of each iteration allowing the same token to be used to represent multiple variables.

Final Class

The final class cannot be extended(inherited). There are two uses of a final class:

  • One is definitely to prevent inheritance, as the final classes cannot be extended.
  • The other use of final with classes is to create an immutable class.

Example:

final class A{}

class B extends A{} // compiler time error

Final Method

A final method can’t be overridden.

Example:

class A{

final void m1(){

System.out.println(“final”);

}

}

class B extends A{

void m1(){

System.out.println(“Hello”); // compiler time error

}

}

--

--

Manisha Shukla
Manisha Shukla

Written by Manisha Shukla

I write on software testing( Manual testing ,Automation,API, Java etc.)