10

I have been studying for my Software Development course and came across the question from a sample:

"Why does it make no sense to have both the static and final modifiers in front of a Java method?"

I have had a bit of a research and everywhere I go it says it is not bad practice and there are good reasons for doing so - for example, this stackoverflow question: Is it a bad idea to declare a final static method?

So, is this question itself nonsensical or is there a legitimate answer to this question?

(There are no given solutions to this sample paper)

Community
  • 1
  • 1

4 Answers4

8

static methods cannot be overriden since they're associated not with an instance of class, but with the class itself. For example, this is how you'd usually call static method:

  MyClass.myStaticMethod()

And this is how you call an instance method:

  new MyClass().myInstanceMethod()

final modifier is used with methods to disallow their override in extending classes.

Andrew Logvinov
  • 21,181
  • 6
  • 52
  • 54
5

Because a static method cannot be overridden. There is therefore no point in marking it final.

Note however that static final variables (which are, oddly, therefore NOT variables because they cannot change) are very useful because their values can be inlined by the compiler.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
1

Static methods can be sort of overridden (though that's not the technical term), since it is resolved at runtime, searching upwards in class chain until it's found. But this "feature" is probably a mistake; people don't use it, people don't know about it, we should pretend it doesn't exist.

irreputable
  • 44,725
  • 9
  • 65
  • 93
0

From the Java Language Spec:

A class method is always invoked without reference to a particular object. It is a compile-time error to attempt to reference the current object using the keyword this or the keyword super.

So you cannot override a static method because it does not belong to an instance. So, the keywords this and super are not avaliable and you cannot use virtual method invocation. And if you cannot use virtual method invocation then the final keyword is of no use.

I like to think that the compiler sees method declarations like this:

public class SomeClass{

    // public static classMethod() becomes
    public static [final] void classMethod(){
        //...
    }

    // and public void instanceMethod() becomes
    public void instanceMethod(SomeClass this, Object super){
        //....
    }

}

public class SomeOtherClass extends SomeClass{

    // overrides
    @Override
    public void instanceMethod(SomeOtherClass this, SomeClass super){
        //...
    }
}

And you call SomeClass instance = new SomeOtherClass().instanceMethod(); then its called the instanceMethod() of SomeOtherClass.

So the compiler does not need to copy method bodys and just pass the reference to the current object in the thread. So, when you use virtual method invocation, in fact you are calling the instanceMethod with a reference to the current object (this) and the body method of the current class is what is called.

ElderMael
  • 7,000
  • 5
  • 34
  • 53