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.

public class Employee
{
  int age = 45;	                    // non-static variable
  static double salary = 8877.66;   // static variable

  public static void display()	    // static method
  {
    System.out.println("Hello World");
  }

  public static void main(String args[])
  {
    // System.out.println(age);     // raises error, it is non-static and requires object
    System.out.println(salary);     // works fine as salary is static, object not required
    display();		            // works fine as display() is static, object not required
  }
}

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.

public class Bank
{
  public static void main(String args[])
  {
    int rupees = 500;		    // non-final can be reassigned
    System.out.println("rupees before reassigned: " + rupees);
    rupees = 600;		    // non-final
    System.out.println("rupees after reassigned: " + rupees);

    final double $rate = 61.5;	    // final cannot be reassigned
    System.out.println("$ Rate is Rs." + $rate);
    // $rate = 62.8;		    // raises error
  }
}

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.

class Manager
{
  public final void doSomeThing()   // observe final
  {
    System.out.println("Do it right now");
  }
}
public class Worker extends Manager
{
  public static void main(String args[])
  {
    Worker w1 = new Worker();
    w1.doSomeThing();
  }
}

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.

final class Fruit		   // observe, final class
{
  String nature = "Seedless";
}
public class Mango                 // extends Fruit raises compilation error
{
  public static void main(String args[])
  {
    Fruit f1 = new Fruit();
    System.out.println("Fruit type is " + f1.nature);
  }
}  

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

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

Leave a Comment

Your email address will not be published.