Java – Final vs Static Access Modifier
Last Updated :
14 Sep, 2023
Final keyword is used in different contexts. First of all, final is a non-access modifier applicable only to a variable, a method, or a class. Following are different contexts where final is used. While 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.
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
import java.io.*;
import java.util.*;
class P {
public void firstName()
{
System.out.println( "Mayank" );
}
public void surName()
{
System.out.println( "Trivedi" );
}
}
class C extends P {
public void surName()
{
System.out.println( "Sharma" );
}
public static void main(String[] args)
{
System.out.println( "GFG" );
}
}
|
Example 2:
Java
import java.io.*;
import java.util.*;
class P {
public void firstName()
{
System.out.println( "Mayank" );
}
public final void surName()
{
System.out.println( "Trivedi" );
}
}
class C extends P {
public void surName()
{
System.out.println( "Sharma" );
}
public static void main(String[] args)
{
System.out.println( "GFG" );
}
}
|
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
import java.io.*;
import java.util.*;
class GFG {
static int x = 10 ;
int y = 20 ;
public static void main(String[] args)
{
GFG t1 = new GFG();
t1.x = 88 ;
t1.y = 99 ;
GFG t2 = new GFG();
System.out.println(
"Value of Static variable x = " + t2.x + "\n"
+ "Value of Instance variable y = " + t2.y);
}
}
|
Output
Value 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.
This modifier is applicable to both outer and inner classes, variables, methods, and blocks. |
This modifier is only applicable to inner classes, methods, and variables. |
It is not necessary to initialize the final variable at the time of its declaration. |
It is necessary to initialize the static variable at the time of its declaration. |
Final variable cannot be reinitialized. |
Static variables can be reinitialized. |
Final method cannot be inherited. |
Static methods can only access the static members of the class and can only be called by other static methods. |
Final class can’t be inherited by any class. |
The static class object can’t be created and it only contains static members only. |
Final keyword doesn’t support any block for initialization of final variables. |
Static block is used to initialize the static variables. |
Final local variables are allowed. |
Unlike C/C++, static local variables are not allowed in Java. |
Final access modifier does not allow the use of OOP concepts like Inheritance and polymorphism. |
Static access modifier allow the use of OOP concepts within static blocks of code. |
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...