• Post Reply Bookmark Topic Watch Topic
  • New Topic

static method ques  RSS feed

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



The above code produces the output

a a a


As I understand the output should be


a d a

Cant get to understand how it produces the first output

Thanks
 
Bartender
Posts: 3307
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The comments in your code state why you will get output you do, is this a homework question?
BTW it's generally consider bad practice to call static methods from an object reference.

Try calling doStuff() via the class name ie Anumal.doStuff(), Dog.doStuff().
 
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree

// it's a redefinition,
// not an override
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sai rama krishna wrote:I agree

// it's a redefinition,
// not an override



It's true that the method is not overridden. I believe the term the JLS uses is "hiding". There's "hiding", "shadowing", and I think one more similar term. They describe similar situations, but I don't recall the finer details of which term applies to which cases.
 
author
Marshal
Posts: 23439
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please QuoteYourSources.


As another hint, even though another rancher kinda gave you the answer, what is the type of the reference being used to call the method?

Henry
 
Java Cowboy
Sheriff
Posts: 16083
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:There's "hiding", "shadowing", and I think one more similar term.


There's hiding, shadowing and obscuring. For day-to-day programming, it's not that important to know the exact rules for these (you can look them up if you get into a situation where they play a role).
 
Marshal
Posts: 58436
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesper de Jong wrote: . . . For day-to-day programming, it's not that important to know the exact rules for these . . .

Jesper is right. It is more important to know how to avoid shadowing hiding and obscuring.
 
Ranch Hand
Posts: 153
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes , As Henry suggested , static method require only reference variable , So they check which Class reference is used and not the object associated with that reference which is reverse of instance method.

Again if you put like Animal a=null; then also it will give you proper result .

So obviously Animal [] a variable is used so always Animal's method will be called.

Correct me if i missed something..

Thanks ..


- Chetan
 
Campbell Ritchie
Marshal
Posts: 58436
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Chetan Dorle wrote: . . . Animal's method will be called. . . .

- Chetan

That looks correct to me
But I still think it is better to follow the advice in Bloch and Gafter’s Java Puzzlers book and avoid hiding shadowing and obscuring as far as possible. This thread shows exactly why that is good advice.
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the output what it is, because the methods are redefined and not over-ridden.

Would the output be different, if the methods were non-static and hence could be over-ridden.
 
Campbell Ritchie
Marshal
Posts: 58436
178
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rajiv Rai wrote: . . . Would the output be different, if the methods were non-static and hence could be over-ridden.

Try it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rajiv Rai wrote:Is the output what it is, because the methods are redefined and not over-ridden.



Sort of. It's part of the same set of rules. You're getting the output you are because for static methods, which class's version of the method to invoke is determined at compile time, by the type of the reference. I believe this was already stated a couple of times here. Is there some part of it you don't understand?

I would say that the fact that the method ends up being hidden, rather than overridden, is a consequence of the above rule, rather than the root cause of what you're seeing.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!