The main difference between static and final is that the static is used to define the class member that can be used independently of any object of the class. In contrast, final is used to declare a constant variable or a method that cannot be overridden or a class that cannot be inherited.
Static and final are two keywords that are used in many Object Orientation supporting programming languages such as Java. Static is used with variables and methods to define that it belongs to the class, not the object. On the other hand, final is used to restrict the user from accessing a variable, method or a class.
Key Areas Covered
1. What is static
– Definition, Functionality
2. What is final
– Definition, Functionality
3. What is the Difference Between static and final
– Comparison of Key Differences
Key Terms
Class, Final Variable, Static Variable
What is Static
A class consists of variables and methods. An object is created from a class; this object can be used to call variable and methods. When a class member is declared with static, it is not required to create an object to call methods and variables. Instead, the class name can be used to call it. In other words, static is a keyword that belongs to the class rather than to the object.
A variable with the static keyword is called a static variable. They are used to refer a common property of a collection of objects. These variables get memory at the time of loading the class. The main advantage of a static variable is that it helps to save memory.
In the above program, there is a static variable called count. In the constructor, the count is incremented by 1. In the main program, three student objects are created. Printing the count will give the result 3 as there are three objects. The count variable is shared by all objects. Each time when an object is created, the count is incremented by one. When displaying the count, it should be written with the class name (e.g. – Student.count).
A method with the static keyword is known as a static method. An example is as follows.
In the above program, square is a static method. It receives an integer value. In the main method, the static method is called, and the value 4 is passed. The answer from the method is stored in the variable result and is finally printed. Here, the class name is used to access the static method. (e.g – Calculate.square(4)). On the other hand, the static methods cannot use non-static data members or call non-static methods directly. Assessing a non-static variable by a static method will give a compile time error.
What is Final
Final is a keyword to restrict the user. It can be used in variables, methods, and classes. A value of a final variable cannot be changed.
In the above program, the variable speedLimit is declared as final. Therefore, its value cannot be changed in the drive method. So, it displays an error.
In the above program, class A has a final method called display. Class B extends A. So, class B can inherit all the variables and methods of class A. Class B also has the method display. As the class A display method is final, it does not allow to override that method in class B.
Moreover, a final class cannot be extended as follows.
As the class A is final, it cannot be extended or inherited by class B.
Difference Between static and final
Definition
Static is a keyword that denotes a member variable or a method that can be accessed without requiring an instantiation of the class to which it belongs. In contrast, final is a keyword that denotes an entity that can only be assigned once. Thus, this explains the fundamental difference between static and final.
Variables
While static variables can be initialized again, final variables cannot be initialized again. This is a major difference between static and final.
Methods
Static methods can be called by other static methods and only access the static members of the class. On the other hand, final methods cannot be overridden. This is another important difference between static and final.
Class
An object cannot be created from a static class; it only consists of static members. Similarly, final classes cannot be inherited by other classes.
Conclusion
Static is used to define the class member that can be used independently of any object of the class. Final is used to declare a constant variable or a method that cannot be overridden or a class that cannot be inherited. This is the main difference between static and final.
Reference:
1. “Static Keyword in Java – Javatpoint.” Www.javatpoint.com, Available here.
2. “Final Keyword in Java – Javatpoint.” Www.javatpoint.com, Available here.
Leave a Reply