Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behaviour of final static method

People also ask

What is a final static method?

The static keyword means the value is the same for every instance of the class. The final keyword means once the variable is assigned a value it can never be changed. The combination of static final in Java is how to create a constant value.

What is the common Behaviour between static and final variable?

It is compulsory to initialize the final variable at the time of its declaration. The static variable can be reinitialized. The final variable can not be reinitialized. Static methods can only access the static members of the class, and can only be called by other static methods.

Can we final the static method?

As we know, static methods cannot be overridden, as they are associated with class rather than instance.


Static methods cannot be overridden but they can be hidden. The ts() method of B is not overriding(not subject to polymorphism) the ts() of A but it will hide it. If you call ts() in B (NOT A.ts() or B.ts() ... just ts()), the one of B will be called and not A. Since this is not subjected to polymorphism, the call ts() in A will never be redirected to the one in B.

The keyword final will disable the method from being hidden. So they cannot be hidden and an attempt to do so will result in a compiler error.

Hope this helps.


static methods cannot be overriden

This is not exactly true. The example code really means that the method ts in B hides the method ts in A. So its not exactly overriding. Over on Javaranch there is a nice explanation.


Static methods belong to the class, not the instance.

A.ts() and B.ts() are always going to be separate methods.

The real problem is that Java lets you call static methods on an instance object. Static methods with the same signature from the parent class are hidden when called from an instance of the subclass. However, you can't override/hide final methods.

You would think the error message would use the word hidden instead of overridden...


You might find yourself in the position to think about making a static method final, considering the following:

Having the following classes:

class A {
    static void ts() {
        System.out.print("A");
    }
}
class B extends A {
    static void ts() {
        System.out.print("B");
    }
}

Now the 'correct' way to call these methods would be

A.ts();
B.ts();

which would result in AB but you could also call the methods on instances:

A a = new A();
a.ts();
B b = new B();
b.ts();

which would result in AB as well.

Now consider the following:

A a = new B();
a.ts();

that would print A. That might surprise you since you are actually having an object of class B. But since you're calling it from a reference of type A, it will call A.ts(). You could print B with the following code:

A a = new B();
((B)a).ts();

In both cases the object you have is actually from class B. But depending on the pointer that points to the object, you will call method from A or from B.

Now let's say you are the developer of class A and you want to allow sub-classing. But you really want method ts(), whenever called, even from a subclass, that is does what you want it to do and not to be hidden by a subclass version. Then you could make it final and prevent it from being hidden in the subclass. And you can be sure that the following code will call the method from your class A:

B b = new B();
b.ts();

Ok, admittetly that is somehow constructed, but it might make sense for some cases.

You should not call static methods on instances but directly on the classes - then you won't have that problem. Also IntelliJ IDEA for example will show you a warning, if you call a static method on an instance and as well if you make a static method final.