| 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: 32599
|
|
|
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: 2770
|
|
|
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
Ranch Hand
Joined: May 22, 2012
Posts: 380
|
|
Another weak reason is the wish to support this transition:
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4722
|
|
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?
|
 |
Joe Areeda
Ranch Hand
Joined: Apr 15, 2011
Posts: 181
|
|
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: 4722
|
|
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.
|
|
|