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.
About Question com.enthuware.ets.scjp.v6.2.740 :
Moderator: admin
-
- Posts: 57
- Joined: Sat Mar 01, 2014 1:48 pm
- Contact:
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.740 :
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.
HTH,
Paul.
-
- Posts: 57
- Joined: Sat Mar 01, 2014 1:48 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.740 :
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.
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");
}
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.740 :
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.
Who is online
Users browsing this forum: No registered users and 4 guests