Difference Between static and final in Java

Each programming language has a specific syntax. The programmer should follow these syntaxes when writing programs. The keywords of programming languages have specific meanings according to the tasks. They are provided by the programming language and cannot be used for user-defined variables, methods, classes, etc. The static and final are two keywords in Java. This article discusses the difference between static and final in Java. The key difference between static and final in Java is that static is used to define the class member that can be used independently of any object of the class while final is used to declare a constant variable or a method that cannot be overridden or a class that cannot be inherited.

CONTENTS

1. Overview and Key Difference
2. What is static in Java
3. What is final in Java
4. Similarities Between static and final in Java
5. Side by Side Comparison – static vs final in Java in Tabular Form
6. Summary

What is static in Java?

A class consists of data members (attributes) and methods. In order to call the methods, there should be an object of that specific class. When a method is declared as static, it is not needed to create an object to call that method. The method can be called using the class name. Refer the below program.

Figure 01: Java Program with static variables and static Method

According to the above program, class A contains number variable and display method. Both are static members. Therefore, it is not necessary to create an object to access the number variable and display method. The programmer can directly write the class name to print the number and to call the method display. So, there is no need to instantiate an object.  If the number variable and display method are non-static, then there should be an object of type A.

Figure 02: Use of static Block

The above program contains the static block and the main method. The static block is called when the class is loaded. Therefore, the statement in the static block executes before the statement in the main block.  If there are many static blocks, they will execute in sequence.

What is final in Java?

In the program, there can be variables of various types. If there is a variable as int x=1; later in the program, that variable value can be changed to some other value. A variable that is declared as final and initialized with a value cannot be changed later in the program.

Figure 03:  Program with final Variable and Inheritance

According to the above program, x is a final variable. It is assigned a value 5. It cannot be changed some other value because it is declared as final. Java supports Object-oriented programming (OOP). One pillar of OOP is a polymorphism. One type of polymorphism is overriding. Class A has the display method. The class B extends class A and it has its own display method. When creating an object of type B and calling the display method will print “B” as the output. The display method of class A is overridden by the display method of class B.

If the programmer what to avoid overriding a method, then he can use the final keyword for that method. If the display method in class A is final, the display method in B will give an error because that method cannot be overridden.

Figure 04: final keyword in the Method

Another pillar of OOP is inheritance. It helps to reuse the already existing code. The new class can extend from the existing class and use the data members and methods of the existing class. If it is required to stop inheriting a class, the programmer can use the keyword ‘final’. Refer the below program.

Figure 05: final keyword in the Class

According to the above program, class A is declared as final. When class B extends A, it gives an error because class A is declared as final. It cannot be inherited by other classes.

What is the Similarity Between static and final in Java?

  • Both static and final are keywords in Java.

What is the Difference Between static and final in Java?

static vs final in Java

Static keyword denotes that a member variable, or method, can be accessed without requiring an instantiation of the class to which it belongs. The final keyword denotes an entity that can only be assigned once.
 Variables
The static variables can be reinitialized. The final variables cannot be reinitialized.
Methods
Can be called by other static methods and only access the static members of the class. The final methods cannot be overridden.
Class
The static class object cannot be created. It only contains static members only. The final class cannot be inherited by other classes.
Block
The static keyword can be used in a block. The final keyword is not used with a block.

Summary – static vs final in Java

This article discussed two keywords in Java such as static and final. The difference between static and final in Java is that static is used to define the class member that can be used independently of any object of the class while final is used to declare a constant variable or a method that cannot be overridden or a class that cannot be inherited. 

Reference:

1.What is Static Keyword in Java | static method and static variable, Telusko Learnings, 6 Mar. 2015. Available here  
2.7.16 How to use Static Block in Java Tutorial, Telusko Learnings, 30 Apr. 2015. Available here
3.8.13 How to use Final Keyword in Java | Method , class and variable, Telusko Learnings, 26 Feb. 2015. Available here