Java – Final vs Static Access Modifier
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 y 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.
Static Access Modifier
Static access modifier is an access modifier that is applicable for methods and variables but not for classes. We can 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.
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.
|Final Access Modifier||Static Access Modifier|
|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 can’t 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.|