Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.740 :

Posted: Wed Aug 03, 2016 12:09 pm
by TwistedLizard
From the enthuware commentry:

"While declaring a method, static usually implies that it is
also final, this is not true for classes."

Although a class with a static method, not explicitly declared final, compiles, is it even possible to construct a situation where a static method is overridden?

As static methods don't override when one class extends another, I don't see how that could come about.

Re: About Question com.enthuware.ets.scjp.v6.2.740 :

Posted: Wed Aug 03, 2016 8:52 pm
by admin
A static method cannot be overridden. In that sense, it is final. But if you declare a nested class as static, you can still extend that class.

HTH,
Paul.

Re: About Question com.enthuware.ets.scjp.v6.2.740 :

Posted: Thu Aug 04, 2016 3:16 am
by TwistedLizard
Thanks Paul.

So although there is no way to override a static method, explicitly declaring one as final doesn't result in any compile time error.

Code: Select all

final static void doStuff(){
    System.out.println("doStuff");
}

Re: About Question com.enthuware.ets.scjp.v6.2.740 :

Posted: Thu Aug 04, 2016 10:37 pm
by admin
Yes, ideally they should disallow the final keyword for static method because static methods are never overridden. However, if you make a static method final, that will prevent you from adding the same method in a subclass. The error message from the compiler that you will get in that case is, "overridden method is static,final". So in that sense, static methods are not final by default.