Difference Between Static Modifier and Final Modifier

Introduction

Sometimes, a programmer might need to define a class member that will be used independently of any object of that class. However, it is possible to create a member that can be used by itself without a reference to any object. To create such a member, the member variable declaration has to be preceded by the keyword static. When a member is declared as static, it can be accessed before any objects of that class are created. For example, the main() in a class is declared static so that it can be involved by the Java runtime system without creating an instance of that class.

The word static can be applied to variables, methods, and even a piece of code that does not belong to a method. When objects of its class are declared, no copy of the static variable is made. All instances of the class share the same static variables. There is only one copy of the variables. A static variable can be referred to by its class name or through a reference to any instance of the class. However, it is advisable to refer to static variables through their class names.
There are certain restrictions when methods are declared as static. They are:

  • They can only call other static methods.
  • They must only access static data.
  • The keyword this or super cannot be used.

For Example, it shows a class that has a static method, some static variables, and a static initialization block.

public class InchesToFeet {

    private static final int inches = 12;

    protected InchesToFeet() {
    }

    public static double convert(double in) {
        return (in / inches);
    }

    static {
        System.out.println("This is a static block executed first");
    }

    public static void main(String[] args) {
        double inch = 66;
        double feet = InchesToFeet.convert(inch);
        System.out.println(inch + " inches = " + feet + " feet.");
    }
}

Output

As soon as this program is loaded, all the static statements are run. First, inches are set to 12, and then the static block is executed. The variable inches will hold a constant value until the programmer wants to change. Next, the convert () method returns a double value with whatever value in inches is passed to it. The main() calls the static method convert () and passes the value in inches.

The main() method is called the static method, convert(), with a dot notation. This way, a static method can be called without instantiating any object of that class. The format is:

classname.methodname();

Final Modifier

Sometimes, a programmer might need to define a class member that will not change. The final modifier can be applied to classes, methods, and variables that cannot have their content modified. So, a final variable has to be initialized as soon as it is declared . Variables declared as final do not occupy memory on a per-instance basis. In other words, it can be said that final variables are like constants. For example,

  • final int RED = 1;
  • final int GREEN = 2;
  • final int BLUE = 3;

It is a common practice to choose uppercase identifiers for final variables.

When the final keyword is applied to a method .it means that it cannot be overridden. If an object has been declared as final, then the reference of the object should not change, but its value can change.

class Box {
    int height;

    Box(int h) {
        height = h;
    }

    public static void main(String[] args) {
        final Box boxObj = new Box(25);
        boxObj.height = 32;
    }
}

Output

This Java code defines a class named Box with a single instance variable height, which represents the height of a box. The class has a constructor that takes an integer parameter h to initialize the height of the Box object. In the main method, a Box object boxObj is created using the new keyword with an initial height of 25. Although boxObj is declared as final, which means the reference to the object cannot be changed (i.e., you cannot assign a new object to boxObj), the object’s internal state can still be modified. Therefore, the code changes the height of the boxObj to 32 after its creation. The line that attempts to reassign boxObj to a new Box object (commented as ERROR!!) would produce an error because a final object reference cannot be reassigned.

Summary

The static and final modifiers serve different purposes. The static modifier is used to declare class-level variables or methods, meaning they belong to the class rather than instances of the class. A static variable is shared across all objects of the class, and a static method can be called without creating an instance of the class. On the other hand, the final modifier is used to restrict the modification of variables, methods, or classes. A final variable can only be assigned once, making it a constant. A final method cannot be overridden by subclasses, and a final class cannot be subclassed.