Java - Final vs Static Access Modifier
Last Updated :
07 Apr, 2025
The final keyword is used in different contexts. First, final is a non-access modifier applicable only to a variable, a method, or a class. The following are different contexts where the final is used. The static keyword in Java is mainly used for memory management. The static keyword in Java is used to share the same variable or method of a given class. The users can apply static keywords with variables, methods, blocks, and nested classes.
Difference Between final and static Modifiers
Final Access Modifier | Static Access Modifier |
---|
It applies to classes, methods, and variables. | It applies to variables, methods, and nested classes. |
The final variable must be initialized at the time of declaration or in the constructor. | static variable can be initialized at the time of declaration or in a static block. |
The final variable cannot be reassigned after it is initialized. | We can reassign the static modifier. |
A final method cannot be overridden by subclasses. | static method can only access static members of the class and cannot be overridden. |
The final class cannot be extended by any other class. | We can add that the static class can exist independently of an instance of the enclosing class. |
It does not support initialization blocks for variables. | A static block is used to initialize static variables or perform static initialization. |
final local variables are allowed and must be initialized before use. | static local variables are not allowed in Java (unlike C/C++). |
It restricts inheritance and polymorphism for classes and methods. | It allows shared behavior and data across all instances of a class. |
Final Access Modifier
Final access modifier is a modifier applicable to classes, methods, and variables. If we declare a parent class method as final then we can’t override that method in the child class because its implementation is final and if a class is declared as final we can’t extend the functionality of that class i.e we can’t create a child class for that class i.e inheritance is not possible for final classes. Every method present inside the final class is always final by default but every variable present inside the final class need not be final. The main advantage of the final keyword is we can achieve security and we can provide a unique implementation. But the main disadvantage of the final keyword is we are missing key benefits of OOPs like Inheritance(Because of the final class), Polymorphism(Because of the final method) hence if there are no specific requirements then it is not recommended to use the final keyword.
Example 1:
Java
// Java Program to illustrate Final keyword
// Where No final keyword Is Used
// Importing required classes
import java.io.*;
import java.util.*;
// Class 1
// Super-class
class P {
// Method 1
// To declare first name
public void firstName()
{
// Passing name and print it
System.out.println("Mayank");
}
// Method 2
// To declare last name
public void surName()
{
// Passing name and print it
System.out.println("Trivedi");
}
}
// Class 2
// Sub-class
// Extending above class
class C extends P {
// Method 1
// Trying to override the last name
public void surName()
{
// Display surname
System.out.println("Sharma");
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Display message
System.out.println("GFG");
}
}
Example 2:
Java
// Java Program to illustrate Final keyword
// When final keyword Is Used
// Importing required classes
import java.io.*;
import java.util.*;
// Class 1
// Super-class
class P {
// Method 1
// To declare first name
public void firstName()
{
// Passing name and print it
System.out.println("Mayank");
}
// Method 2
// To declare last name
public final void surName()
{
// Passing name and print it
System.out.println("Trivedi");
}
}
// Class 2
// Sub-class
// Extending above class
class C extends P {
// Method 1
// Trying to override the last name
public void surName()
{
// Display surname
System.out.println("Sharma");
}
// Method 2
// Main driver method
}
public class Geeks
{
public static void main(String[] args)
{
// Creating object of sub-class
C obj = new C();
// Calling first name method
obj.firstName();
// Calling last name method
obj.surName();
}
}
Output:
Static Access Modifier
Static access modifier is an access modifier that is applicable for methods and variables but not for classes. We can not declare top-level class with a static modifier but we can declare the inner class as static (such types of inner classes are known as static nested classes). In the case of instance variable for every object, a separate copy will be created but in the case of static variable, a single copy will be created at class level and shared by every object of that class.
Example:
Java
// Java Program to Illustrate Static Access Modifier
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
class Geeks {
// Creating a static variable and
// initializing a custom value
static int x = 10;
// Creating a instance variable and
// initializing a custom value
int y = 20;
// Main driver method
public static void main(String[] args)
{
// Creating an object of class inside main() method
Geeks t1 = new Geeks();
// Accessing and re-initializing the
// static and instance variable
// using t1 reference
t1.x = 88;
t1.y = 99;
// Creating an object of class inside main() method
// again
Geeks t2 = new Geeks();
// Accessing the static and instance variable using
// t2 reference as we know that for each object
// there is a separate copy of instance variable
// created. While a same copy of static variable will
// be shared between the objects
// Displaying the value of static and instance
// variable using t2 object reference
System.out.println(
"Value of Static variable x = " + t2.x + "\n"
+ "Value of Instance variable y = " + t2.y);
}
}
OutputValue of Static variable x = 88
Value of Instance variable y = 20
After having a good understanding and implementation of both of the above specifiers and these important special keywords let us tabulate differences between them to grasp a good understanding over these keywords.
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read