---
title: "What is the difference between static and final keywords in Java?"  
description: "What is the difference between static and final keywords in Java?"  
author: "Ravi Vishwakarma"  
published: 2024-07-18  
updated: 2024-07-18  
canonical: https://www.mindstick.com/interview/33950/what-is-the-difference-between-static-and-final-keywords-in-java  
category: "java"  
tags: ["javascript", "java", "javac"]  
reading_time: 3 minutes  

---

# What is the difference between static and final keywords in Java?

The `static` and `final` keywords serve distinct purposes and are used in different contexts.

Here's a detailed explanation of the differences between them:

#### `static` Keyword

The `static` keyword is used to indicate that a member (variable, method, or nested class) belongs to the class itself, rather than to instances of the class.

#### `final` Keyword

The `final` keyword is used to restrict the modification of variables, methods, and classes.

## Summary of Differences:

| Feature | `static` | `final` |
| --- | --- | --- |
| **Purpose** | Class-level variable, method, block, or nested class | Restricts modification of variables, methods, and classes |
| **Variable** | Shared among all instances of the class | Value cannot be changed once initialized |
| **Method** | Can be called without an instance | Cannot be overridden |
| **Class** | Enables nested class without reference to outer class | Cannot be subclassed |
| **Initialization Block** | Static block executed when class is loaded | Not applicable |

### Example Combining Both Keywords:

You can use both `static` and `final` together for class constants:

```java
public class Constants {
    public static final int MAX_USERS = 100;
    public static final String APP_NAME = "MyApp";
}
```

In this example, `MAX_USERS` and `APP_NAME` are constants that are shared across all instances of the class and cannot be modified.

**Read more** - [**Java program to find all pairs of elements in an array whose sum is equal to a given number.**](https://www.mindstick.com/interview/33947/java-program-to-find-all-pairs-of-elements-in-an-array-whose-sum-is-equal-to-a-given-number)

## Answers

### Answer by Ravi Vishwakarma

The `static` and `final` keywords serve distinct purposes and are used in different contexts.

Here's a detailed explanation of the differences between them:

#### `static` Keyword

The `static` keyword is used to indicate that a member (variable, method, or nested class) belongs to the class itself, rather than to instances of the class.

#### `final` Keyword

The `final` keyword is used to restrict the modification of variables, methods, and classes.

## Summary of Differences:

| Feature | `static` | `final` |
| --- | --- | --- |
| **Purpose** | Class-level variable, method, block, or nested class | Restricts modification of variables, methods, and classes |
| **Variable** | Shared among all instances of the class | Value cannot be changed once initialized |
| **Method** | Can be called without an instance | Cannot be overridden |
| **Class** | Enables nested class without reference to outer class | Cannot be subclassed |
| **Initialization Block** | Static block executed when class is loaded | Not applicable |

### Example Combining Both Keywords:

You can use both `static` and `final` together for class constants:

```java
public class Constants {
    public static final int MAX_USERS = 100;
    public static final String APP_NAME = "MyApp";
}
```

In this example, `MAX_USERS` and `APP_NAME` are constants that are shared across all instances of the class and cannot be modified.

**Read more** - [**Java program to find all pairs of elements in an array whose sum is equal to a given number.**](https://www.mindstick.com/interview/33947/java-program-to-find-all-pairs-of-elements-in-an-array-whose-sum-is-equal-to-a-given-number)


---

Original Source: https://www.mindstick.com/interview/33950/what-is-the-difference-between-static-and-final-keywords-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
