What is the Final Keyword in Java?
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 of the Final Keyword in Java
Example 1: Final Variable
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
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.
What is the Static Keyword in Java?
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 of the Static Keyword in Java
Example 1: Static Variable
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
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
Frequently Asked Questions
Can a static method be overridden?
No, a static method cannot be overridden in Java. It belongs to the class, not the instance, and is resolved at compile-time.
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.
Recommended Readings: