![]() |
VOOZH | about |
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.
| 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 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:
GFG
Example 2:
Output:
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:
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.