• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

for static

 
Ranch Hand
Posts: 69
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Running this code produces the output:

a a a



why the output is a a a ???
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are expecting to overriding take place and produce different output, remember overriding is applicable to "objects", static methods are class level.
 
munjal upadhyay
Ranch Hand
Posts: 69
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ramesh maredu wrote:I think you are expecting to overriding take place and produce different output, remember overriding is applicable to "objects", static methods are class level.



I know that., this is the example of the SCJP by kathy . but why the output is a a a ?
It must be a d a .!
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

munjal upadhyay wrote:but why the output is a a a ?
It must be a d a .!


really? I ask you one question - can we override a static method ? also *google about redefining vs overriding*
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because it is static method, overriding doesn't take place, when you say a.doStuff(), because doStuff() is static method compiler replaces reference with class name, so here after compiling code will be A.doStuff(), so it invokes A version of doStuff().
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now got your confusion! the a[index] really is a reference of declared type i.e, Animal . it is not an object itself
 
munjal upadhyay
Ranch Hand
Posts: 69
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:now got your confusion! the a[index] really is a reference of declared type i.e, Animal . it is not an object itself



thanks I get it..
 
munjal upadhyay
Ranch Hand
Posts: 69
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry to intrupt again !!!
but if we cannot override the static method then what is this ?


output ...
in A
in B
in A
in B
in B
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A and B has their own and separate methodA(), the method in B is redefined, you are seeing out "in B" because you are calling methodA() in class B.

b.methodA() and B.methodA() both produces same output as after compilation reference would be replace with class name for static methods.
 
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic