Table of contents
1.
Introduction
2.
Final Access Modifier
2.1.
What is final KEYWORD?
2.2.
Syntax
2.3.
Examples
2.3.1.
Example 1: Final Variable
2.4.
Java
2.4.1.
Example 2: Final Method
2.5.
Java
3.
Static Access Modifier
3.1.
What is static Keyword?
3.2.
Syntax
4.
Example
4.1.
Example 1: Static Variable
5.
Java
5.1.
Example 2: Static Method
6.
Java
7.
Difference Table between final and static Access Modifiers
8.
Frequently Asked Questions
8.1.
Can a final variable be initialized later?
8.2.
Can a static method access instance variables?
8.3.
What happens if a final class is subclassed?
8.4.
Can static variables be overridden?
9.
Conclusion
Last Updated: Aug 22, 2024
Easy

Static vs Final Keyword in Java

Author Riya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, the static and final keywords are often used but serve different purposes. Understanding how these keywords work can help you write more efficient and effective code. 

Static vs Final Keyword in Java

This article will explore the differences between static and final, their uses, and how they affect the behavior of variables and methods in Java.

Final Access Modifier

What is final KEYWORD?

The final keyword in Java is used to restrict the user from modifying variables, methods, or classes. When applied, final makes a variable constant, a method unchangeable, or a class non-subclassable.

Syntax

final int MY_CONSTANT = 10;
final void myMethod() {
    // method body
}
final class MyClass {
    // class body
}

Examples

Example 1: Final Variable

  • Java

Java

public class FinalVariableExample {

   public static void main(String[] args) {

       final int MY_CONSTANT = 100;

       // MY_CONSTANT = 200; // This will cause a compilation error

       System.out.println(MY_CONSTANT);

   }

}
You can also try this code with Online Java Compiler
Run Code


Output

100


In this example, MY_CONSTANT is declared as final, which means its value cannot be changed once assigned.

Example 2: Final Method

  • Java

Java

class Parent {

   final void display() {

       System.out.println("This is a final method.");

   }

}

class Child extends Parent {

   // void display() { // This will cause a compilation error

   // System.out.println("Cannot override final method.");

   // }

}

public class FinalMethodExample {

   public static void main(String[] args) {

       Child obj = new Child();

       obj.display();

   }

}
You can also try this code with Online Java Compiler
Run Code


Output

This is a final method.


Here, the display method in the Parent class is marked as final, so it cannot be overridden in the Child class.

Static Access Modifier

What is static Keyword?

The static keyword is used to define class-level variables and methods. Unlike instance variables and methods, static variables and methods belong to the class itself rather than any particular instance of the class.

Syntax

static int count = 0;
static void increment() {
    count++;
}

Example

Example 1: Static Variable

  • Java

Java

class Counter {

   static int count = 0;

  static void increment() {

       count++;

   }

}

public class StaticVariableExample {

   public static void main(String[] args) {

       Counter.increment();

       Counter.increment();

       System.out.println(Counter.count);

   }

}
You can also try this code with Online Java Compiler
Run Code


Output

2


Here, the count variable is static, so it is shared across all instances of the Counter class.

Example 2: Static Method

  • Java

Java

class MathUtil {

   static int add(int a, int b) {

       return a + b;

   }

}

public class StaticMethodExample {

   public static void main(String[] args) {

       int result = MathUtil.add(10, 20);

       System.out.println(result);

   }

}
You can also try this code with Online Java Compiler
Run Code


Output

30

Difference Table between final and static Access Modifiers

Feature

Final

Static

PurposePrevents modification (constant value, unchangeable method, non-subclassable class)Shares a single copy across all instances of a class
UsageVariables, methods, classesVariables, methods
InheritanceCannot be overridden in subclassesMethods can be called on the class itself
Instance-specificNoNo
InitializationMust be initialized onceCan be initialized once
Memory AllocationMemory allocated per instanceMemory allocated once for the class

Frequently Asked Questions

Can a final variable be initialized later?

No, a final variable must be initialized when it is declared or within the constructor if it is a member variable.

Can a static method access instance variables?

No, static methods cannot access instance variables directly. They can only access other static members of the class.

What happens if a final class is subclassed?

It causes a compilation error. A final class cannot be subclassed.

Can static variables be overridden?

No, static variables are not overridden but can be shadowed by local variables or subclass variables.

Conclusion

Understanding the static and final keywords in Java is crucial for managing data and methods within your programs. The final keyword helps in creating constants and ensuring that certain methods or classes remain unmodified. On the other hand, static helps in defining class-level variables and methods that are shared across all instances. By grasping the differences and applications of these keywords, you can write more efficient and controlled Java programs.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass