aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static methods" Watch "static methods" New topic
Author

static methods

Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
I remember things better if I understand the underlying reason for the behavior. That said, can anyone explain why static methods cannot be overridden?

Thanks.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Higgledy Smith wrote:I remember things better if I understand the underlying reason for the behavior. That said, can anyone explain why static methods cannot be overridden?

Thanks.

Static methods can be hidden (but not overridden.) Why can't they be overridden? Because overriding uses polymorphism (which calls the correct method based on the actual type of the instance referred to by the reference.) But with static methods you have no underlying instance, since they are linked to the class as a whole. That's why you can't override a static method (but if you declare a static method with the same signature in a subclass, that static method will hide the superclass' static method in subclass code.)


All code in my posts, unless a source is explicitly mentioned, is my own.
victor kamat
Ranch Hand

Joined: Jan 10, 2007
Posts: 247
It is worth noting that static methods cannot be abstract.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
victor kamat wrote:It is worth noting that static methods cannot be abstract.

You are correct. Abstract methods are meant to be overridden, which static methods can not. Surprisingly, however, static methods can be marked final. A static method which is marked final can not be redefined in a subclass.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static methods