Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes When static method does not take part in overriding then why can we declare a throws clause. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "When static method does not take part in overriding then why can we declare a throws clause." Watch "When static method does not take part in overriding then why can we declare a throws clause." New topic
Author

When static method does not take part in overriding then why can we declare a throws clause.

devasis majhi
Greenhorn

Joined: Dec 29, 2012
Posts: 9
When static method does not take part in method overriding then why can't we declare a throws clause in a method of child class to throw a checked exception if the same is not declared in parent class.

Example:-





Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38044
    
  22
Don’t know, but the Java Language Specification says a hiding method must not declare any new checked Exceptions. Just like an overriding method.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2997
    
    9
There's not really any good reason for this, in my opinion. The best reason I can think of is that it makes it easier if you need to refactor a static method to an instance method, or vice versa. Not a very strong reason, in my opinion, but it's something.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Another weak reason is the wish to support this transition:





Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

devasis majhi wrote:When static method does not take part in method overriding then why can't we declare a throws clause in a method of child class to throw a checked exception if the same is not declared in parent class.

Like the others, I don't really know. However, it's worth pointing out that method hiding is not generally a good idea anyway, so I can live with a few logical inconsistencies.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 307
    
    2

I don't really know either but it may have something to do with the throws and the way the compiler checks if you've caught or declared all user exceptions.

I haven't tried it but I wonder if it would let you do that if the base class could also throw the same exception?

Joe


It's not what your program can do, it's what your users do with the program.
devasis majhi
Greenhorn

Joined: Dec 29, 2012
Posts: 9
Same thing applicable to return type also. We cann't have any return type in static method of child class if parent class does not define the same.



Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

devasis majhi wrote:Same thing applicable to return type also. We cann't have any return type in static method of child class if parent class does not define the same...

OK, but I don't really understand why this is such a big deal for you.

The fact is that hiding methods is not a good thing, and the rules are what they are. Agonising about it seems like a poor use of your time.

Winston

[Edit] PS : Please DontWriteLongLines (←click). I've broken yours up this time.
 
 
subject: When static method does not take part in overriding then why can we declare a throws clause.
 
Similar Threads
Static members | Inheritance
Exception question
Exception and Overriding
overriding
Exceptions in general