• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Static method redefinition - Exceptions

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


We are redefining the static function in the subclass. The compiler still gives error over declared exception. Why? Are there any other gotchas we need to know about static for the exam?
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anu,

Please QuoteYourSources for the snippet of code above as its javaranch policy.

I am quoting directly from JLS

Static initializers (§8.7), class variable initializers, and instance initializers or instance variable initializers within named classes and interfaces (§8.3.2), must not result in a checked exception; if one does, a compile-time error occurs. No such restriction applies to instance initializers or instance variable initializers within anonymous classes (§15.9.5).

Please check this

Try changing your code to something like this and it will compile fine

Let me know, if you have more doubts.
In mean while, i am also trying to find the solid reason for your answer

Thanks,
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't detect any redefining att all.
Consider:which results in:

In Tester()
In A()
In A()


If callme() would have been successfully redifined I believe the
result should have been:

In Tester()
In A()
In Tester()


Eclipse thinks that I should access call me in a static way though and
not via an object reference like in the last case.

The evidence and my memory both suggest that it is impossible
to override static methods. (It looks like they have to follow
some rules about similar methods signatures when having the
same name though.)
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


We are not overriding the method and the compiler will interpret the statement as
A.callme()
That's why, it is calling the correct method thats in A. Only thing is that there is some gotcha regarding redefining the static
method in a subclass with the same name. Redefining doesn't mean overriding and yes you are correct, static methods can
never be overriden.

Best Regards,
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I think so this statement might clarify this doubt, i am copying it from this

Actually static methods are not overridden in the true sense. They are 'hidden' by subclass method of same signature and return type. So, they do not participate in dynamic binding. Thus irrespective of which refernce a superclass is holding, it JVM invokes superclass method, if method in question is static.

So i guess these are the rules in case of redefining static methods.

1- The redefined method in subclass must have same signature.
2- It must have same return type.
3- It must now throw any checked exceptions if the super-class method is not throwing in order to properly hide the parent class method.

When we say this


In such a situation, i guess this rule will impy

4- Thus irrespective of which refernce a superclass is holding, it JVM invokes superclass method, if method in question is static.

Why i am saying this because when i have compiled the code in Eclipse, it's giving me the following error


Exception is not compatible with throws clause in A.callme()


Now i am waiting for other fellows to reply. Interesting thread and question Anu.

Best Regards,
 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for sorting out my confused terminology.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome Jonas.

Read my last reply and give your opinion what do you think, am i correct in the specifying the explanation above.

Best Regards,
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:You are welcome Jonas.

Read my last reply and give your opinion what do you think, am i correct in the specifying the explanation above.

Best Regards,



I just begin to hate Java for its ambiguity.Today morning I began to think , now I'm ready for real exam but now I am knowing nothing for sure. Experts where are you ? Any opinion about this tricky question
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Rufat,

It's not Java ambiguity, it's surely our lack of knowledge. As we can't know every traps of a language, that's why
it is rightly said, it takes years of constant worship to master some trait.

Best Regards,
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry was a duplicate reply.
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic