Static and final both are the keywords used in Java. The static member can be accessed before the class object is created. Final has a different effect when applied to class, methods and variables.
The main difference between a static and final keyword is that static is keyword is used to define the class member that can be used independently of any object of that class. Final keyword is used to declare, a constant variable, a method which can not be overridden and a class that can not be inherited.
Content: Static Vs Final in Java
Comparison Chart
Basis for Comparison | Static | Final |
---|---|---|
Applicable | Static keyword is applicable to nested static class, variables, methods and block. | Final keyword is applicable to class, methods and variables. |
Initialization | It is not compulsory to initialize the static variable at the time of its declaration. | It is compulsory to initialize the final variable at the time of its declaration. |
Modification | The static variable can be reinitialized. | The final variable can not be reinitialized. |
Methods | Static methods can only access the static members of the class, and can only be called by other static methods. | Final methods can not be inherited. |
Class | Static class's object can not be created, and it only contains static members only. | A final class can not be inherited by any class. |
Block | Static block is used to initialize the static variables. | Final keyword supports no such block. |
Definition of Static
Static is a keyword, applicable to the classes, variables, methods, and blocks. The class members, class, and blocks can be made static using “static” keyword in front of the name of the class members, class, and blocks respectively. When a class member is declared as static, it becomes global for all other members of the class.
The static member of the class does not occupy memory on per instance basis, i.e. all the objects shares the same copy of static member. The static member can be used independently of any object of that class. You can access the static member of the class before its object is created. The best example of the static member is main( ) method, it is declared static so that it can be invoked before any object exists.
The general form to access the static member of the class:
class_name.static_member // accessing static member of the class
In above code class_name is the name of the class in which the static_member is defined. Static member can be a static variable or static method
Static variables
- A static variable acts like a global variable for all other data members of the class.
- A static variable can be accessed before any object of the class exists.
- A static variable can be accessed with the class name in which it is defined followed by the dot(.) operator.
Static Methods
- A static method can only call other static methods only.
- A static method can access static data only.
- A static method can not be referred to “this” or “super” in any conditions.
- A static method can be accessed with the class name in which it is defined followed by the dot(.) operator.
Static class
- Java does have the concept of nested static class. The outermost class can not be made static whereas the innermost class can be made static.
- A static nested class can not access the non-static member of the outer class.
- It can only access the static members of the outer class.
Static Block
Static block is executed only once when the class is loaded. Used to initialize the static variables of the class.
C++
In C++ we have the concept of static variables as well as static functions whereas, C++ do not support static class.
C#
C# supports static class, static variables, and static Class also.
Java
Java supports static nested class, static variables, static methods.
Definition of Final
Final is a keyword applicable to the class, variable and methods. The class, variable and the method is declared as final using the keyword “final” preceded by their name. Once a variable is declared as final; it can not be modified further in the program. A final variable must be initialized at the time declaration. Final variables do not occupy memory on per-instance basis. All the objects of the classes share the same copy of the final variable.
The method declared as final can not be overridden by the subclass of that class in which final method is declared. When a class is declared as final other class can not inherit that final class. C++, C# do not support the concept if final keyword. Java supports the concept of final keyword and in Java; class, variable, and method can be declared as final.
Key Differences Between Static and Final in Java
- The static keyword is applicable to a nested static class, variables, methods and blocks. On the other hand, final keyword is applicable to class methods and variables.
- Static variable can be initialized any time whereas, a final variable must be initialized at the time of declaration.
- A static variable can be reinitialized whereas, once initialized a final variable can never be reinitialized.
- A static method can access the static member of the class and can only be invoked by other static methods. On the other hand, the final method can never be inherited by any class.
- Static block is used to initialize the static variables whereas, final keyword does not support any block.
Conclusion
Both static and final keyword resolve different purpose when applied to class, variable, and method.
Rohit says
Please tell the difference between inner classes and subclasses.
Neelima P says
We will update the difference soon. Keep viewing .. 🙂
vivek says
the info was very useful.
haynes christian says
Can you tell me the difference between static, final and finalize.
A-aron says
Awesome comparison and contrast, thank you!
Prism says
Great Explanation, Everything covered in this article about static keyword… Great
Abhijit Zende says
Can we make a class or method , final and static together at same time?