Java Made Clear: Difference between static and final Keywords

Java Made Clear: Difference between static and final Keywords

static vs final java, both are known access modifiers in Java doing different functionalities (used for different jobs).
  1. static: static keyword can be applied to instance variables and methods but not to classes. When applied, variables and methods can be called without the help of an object. When a method or variable is called without object, encapsulation is not maintained. That is, with static variables and methods, encapsulation does not exist.
  2. final: final keyword can be applied to all constructs – variables, methods and classes. When applied, final behaves very differently with each with different functionalities.

Let us explore static vs final java programmatically.

I) Usage of static Keyword

Generally, an instance variable or method requires an object to call with. But static variables and methods do not require an object. Observe, the following code.

static vs final java

In the above code, age is non-static and requires an object to call with. Observe, the main() method. salary being static does not require an object. Similarly with static display() method. It is called wihtout the need of an object.

II) Usage of final Keyword

As stated earlier, the final keyword can be applied in three places – variables, methods and classes. When applied it behaves differently.

  1. A final variable cannot be reassigned.
  2. A final method cannot be overridden.
  3. A final class cannot be inherited.

But one thing can be found in common among the above three, "final means something cannot be done".

a) final with variables

A final variable works like a constant of C/C++. That is, a final variable cannot be reassigned. Java does not support const keyword and its place uses final keyword.

static vs final java
The variable rupees is not final and thereby reassigned with a new value of 600. $rate is final and thereby cannot be reassigned.

b) final with methods

If the super class does not permit the subclass to override, it simply declares the method as final. The final methods of super class cannot be overridden by subclass.

static vs final java

In the super class Manager, doSomeThing() is declared final. The subclass Worker is not permitted to override. Try once, it raises compilation error. If the super class have non-final methods (not shown in the code), the subclass can feel free to override at its convenience.

c) final with classes

The programmer may not like a class to be inherited by other classes because he would like to allow the accessibility throgh composition not by inheritance.

static vs final java

In the above code, Fruit is declared as final and thereby Mango cannot extend it. Then how it can make use of Fruit class methods? Simply by creating an object of Fruit and calling Fruit class methods. This is known as composition.

What is Composition in Java?

Creating an object of other class in our class and calling other class variables and methods is known as composition (known as has-a relationship as one class "has a" object of other class). Here, object and method or variable belong to the same class. In contrast, in inheritance object belongs to subclass and variable or method belong to super class.

View All for Java Differences on 90 Topics

4 thoughts on “Java Made Clear: Difference between static and final Keywords

Leave a Reply

Your email address will not be published. Required fields are marked *