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

Target Reference and Static Methods

 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Garret,
well, if a method is static then it is invoked via the class rather than an instance. so actually the java runtime doesn't even use the object reference you return (as null) to invoke the method mountain().
u can try this as well,
Test t = null;
t.mountain();
it will work.
here is the code i wrote,

but when we compile this class and .class file is generated compiler takes care of the static method calls as a part of the compile optimization/simplification(if u would say)...
here is the .class file code after decompiling it via "cavaj" (one of the decompiler)
i have put only the main() method of the TestStaticRef class here..

here as u see favorite() and mountain() methods for TestStaticRef and TestStaticRef1 are called via the class name rather than the instance that u return at run time...
so it works!!
regards
maulin.
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting. Thank you.
 
reply
    Bookmark Topic Watch Topic
  • New Topic