• 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

Static Methods

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why doesn't dynamic binding work with static methods?

For example:

If I have a class



Then say:

Liminal l = new SubLiminal();
l.method();

The method doesn't go and call the subclass version as you would expect with dynamic binding.

Is this because calling static methods on a reference is just a syntax trick, and is much the same as saying Liminal.method(); ?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are not overridden in a subclass. They are hidden. If the superclass and subclass have static methods with the same signature, then the type of reference determines which method is called.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is this because calling static methods on a reference is just a syntax trick, and is much the same as saying Liminal.method(); ?



Yes.

Static methods are invoked based on the type of the reference variable. Instance methods are invoked based on the type of the object that is being referred to.

regards,
vijay.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arthur Blair:
...is much the same as saying Liminal.method(); ?


Indeed, "Class.method()" is the preferred way to call static methods -- as opposed to "instance.method()."

Also see Overriding vs. Hiding.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic