• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

static method ques

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 842
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic