• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic