Difference Between Static and Final in Java



In this article, we will learn about the difference between static and final keywords in Java. Two of the most frequently used and frequently confused keywords are static and final. Although they may at times be used together in code, they have basically different functions.

Static

The static keyword can be applied to nested static classes, variables, methods, and blocks. It is not required to initialize the static variable when it is declared. This variable can be re-initialized. It can access the static members of the class only, and it can be called only by other static methods.

Objects of the static class can't be created, a static class can only contain static members, and it is used to initialize the static variables.

The following are the three main features of the static keyword in Java:

Static Variable

The static variable in Java is a variable whose value can be changed or re-initialized. We can declare the variable static by just writing the static keyword before the variable.

static int var = 3;

Static Method

The static method in Java is a method that belongs to the class rather than instances and can be called without creating an object. It can't access non-static members of the code.

static int add(int a, int b) {
        System.out.println(a + b);
}

Static Block

A static block is a block that initializes a static variable when the class is loaded. It executes once when the class loads the code before the main method.

static {
        System.out.println("Tutorials Point");
}

Example of Static

Below is an example of the final keyword in Java to add two numbers:

public class StaticAddNumbers{
    static int num1 = 5;
    static int num2 = 3;
    public static void main(String[] args) {
        System.out.println(num1 + num2);
    }
}

Output

8

Final

Final is the keyword that we use before a class, variable, or method that we do not want to extend and change. It is used to apply restrictions on classes, methods, and variables. The final keyword can't be inherited, and it can't be overridden.

Final methods can't be inherited by any class. We need to initialize the final variable when it is being declared. The value of a final, once declared, can't be changed or re-initialized.

The following are the 3 main features of the final keyword in Java:

Final variable

The final variable in Java is a variable whose value we can not change or re-initialize. We can declare the variable as final by just writing the final keyword before the variable.

final int var=3;

Final class

The final class in Java is the class that cannot be extended by other subclasses. When we want to use the class as it is without any modification.

final class ClassName {
    // Class function
}

Final method

The final method in Java is a method that ensures method behavior remains unchanged in inheritance, it cannot be overridden by subclasses.

final void display() {
        System.out.println("Tutorials Point");
    }

Example of Final

Below is an example of the final keyword in Java to add two numbers:

public class FinalAddNumbers {
    final int num1 = 5;
    final int num2 = 3;    
    public static void main(String[] args) {
        Adder adder = new Adder();
        int sum = adder.num1 + adder.num2;
        System.out.println("Sum: " + sum);
    }
}

Output

8

Difference between static and final keyword

Below is the difference between the static and final keywords:

Criteria Static Final
Purpose Indicates class-level membership Indicates immutability/restriction
Variable Behavior Single shared copy Value cannot be changed
Method Behavior Belongs to the class, not instances Cannot be overridden
Memory Allocated in the class area Depends on the usage context
Initialization When the class loads Must be initialized before use
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-05-15T11:26:06+05:30

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started