Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
devasis majhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 981
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another weak reason is the wish to support this transition:





 
Winston Gutkowski
Bartender
Pie
Posts: 10504
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joe Areeda
Ranch Hand
Posts: 331
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
devasis majhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10504
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic