---
title: "What is the significance of the 'final' keyword in Java?"  
description: "What is the significance of the 'final' keyword in Java?"  
author: "Ashutosh Patel"  
published: 2024-07-21  
updated: 2024-07-21  
canonical: https://www.mindstick.com/interview/33963/what-is-the-significance-of-the-final-keyword-in-java  
category: "java"  
tags: ["java", "keywords"]  
reading_time: 3 minutes  

---

# What is the significance of the 'final' keyword in Java?

#### Final Keyword in Java

The key word ‘end’ is used to indicate that something cannot always be changed. It varies in importance according to its use:

## Final Variables

When applied to a variable, it means that the value of the variable cannot be changed once it is initialized.

## Syntax-

```java
final int x = 10;
// x = 20; // Compilation error: cannot assign a value to final variable x
```

## Final Methods

Used in a method, it means that subclasses cannot be placed on the method.

## Syntax-

```java
class Parent {
   final void display() {
       System.out.println("Parent's display method");
   }
}
class Child extends Parent {
   // Compilation error: Cannot override the final method from Parent
    void display() {
       System.out.println("Child's display method");
    }
}
```

## Final Classes

When applied to a class, it means that the class cannot be parsed (i.e. no other class can inherit from it).

## Syntax-

```java
final class FinalClass {
   // Class implementation
}
// Compilation error: Cannot inherit from final FinalClass
// class SubClass extends FinalClass {
//     // Class implementation
// }
```

## Final Parameters

When applied to a method parameter, it indicates that the parameter cannot be changed in the method.

## Syntax-

```java
void printValue(final int num) {
   // num = 20; // Compilation error: cannot assign a value to final parameter num
   System.out.println("Value: " + num);
}
```

The '**final**' keyword ensures immutability, prevents method execution, prohibits subclassing, or forces method parameters to remain unchanged within a method This provides code reliability, security, and performance up in Java programs.

**Also, Read:** [What is a HashMap and how does it work?](https://www.mindstick.com/interview/33962/what-is-a-hashmap-and-how-does-it-work)

## Answers

### Answer by Ashutosh Patel

#### Final Keyword in Java

The key word ‘end’ is used to indicate that something cannot always be changed. It varies in importance according to its use:

## Final Variables

When applied to a variable, it means that the value of the variable cannot be changed once it is initialized.

## Syntax-

```java
final int x = 10;
// x = 20; // Compilation error: cannot assign a value to final variable x
```

## Final Methods

Used in a method, it means that subclasses cannot be placed on the method.

## Syntax-

```java
class Parent {
   final void display() {
       System.out.println("Parent's display method");
   }
}
class Child extends Parent {
   // Compilation error: Cannot override the final method from Parent
    void display() {
       System.out.println("Child's display method");
    }
}
```

## Final Classes

When applied to a class, it means that the class cannot be parsed (i.e. no other class can inherit from it).

## Syntax-

```java
final class FinalClass {
   // Class implementation
}
// Compilation error: Cannot inherit from final FinalClass
// class SubClass extends FinalClass {
//     // Class implementation
// }
```

## Final Parameters

When applied to a method parameter, it indicates that the parameter cannot be changed in the method.

## Syntax-

```java
void printValue(final int num) {
   // num = 20; // Compilation error: cannot assign a value to final parameter num
   System.out.println("Value: " + num);
}
```

The '**final**' keyword ensures immutability, prevents method execution, prohibits subclassing, or forces method parameters to remain unchanged within a method This provides code reliability, security, and performance up in Java programs.

**Also, Read:** [What is a HashMap and how does it work?](https://www.mindstick.com/interview/33962/what-is-a-hashmap-and-how-does-it-work)


---

Original Source: https://www.mindstick.com/interview/33963/what-is-the-significance-of-the-final-keyword-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
