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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

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

Post 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");
}

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests