• 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

Help needed!

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, a method call on an object such as x.f() is resolved, when the program executes, not when it is compiled, in order to support polymorphism. Name two situations, where The Java Compiler can determine the exact method to be called before the program executes..?

Please help, so far I am thinking about, if it can have anything to do with casts?
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If f() is a static method, which f() is called is determined by the compiler based on x's declared type.

As for a second case, if f() is declared final in the class that x is declared to be. Heh, that might take an example.I would expect that line 2 will be compiled as a polymorphic lookup of f() since it could be A.f() or B.f() based on the instance x references at runtime.

However, line 5 should be compiled to directly call B.f() since that's the only option for a C (or any subclass of it).

Of course, IANAJVMA, so YMMV.
 
kotoisin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. It helped me.
 
reply
    Bookmark Topic Watch Topic
  • New Topic