• 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

Explain binding

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly is binding in java? I've read about static binding and dynamic binding but i dont have a clear understanding of what binding really is. I want an easy to understand explanation. Thanks in advance
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It just refers to the process of matching up a method name in your source code to the actual method that gets executed at runtime. The rules for how this is accomplished are different for static methods, for final methods, for non-final methods, and for overloaded methods. "Early" or "static" binding basically means that the compiler can figure it out completely; "late" or "dynamic" binding means that the decision isn't made until runtime.
 
Oceana Wickramasinghe
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:It just refers to the process of matching up a method name in your source code to the actual method that gets executed at runtime. The rules for how this is accomplished are different for static methods, for final methods, for non-final methods, and for overloaded methods. "Early" or "static" binding basically means that the compiler can figure it out completely; "late" or "dynamic" binding means that the decision isn't made until runtime.



Thank you very much, but what if i called a static method using a reference variable? Would that still be static binding or dynamic binding?

 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Oceana Wickramasinghe wrote:what if i called a static method using a reference variable? Would that still be static binding or dynamic binding?



Yes. The reference value is ignored -- it can even be null! All that matters is the declared type of that variable; the compiler uses that type, and only that type, to choose the static method. Static methods are never "overridden" -- there's no dynamic lookup, ever.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is why it is bad practice to call static members on an object reference. You should call static members on the name of the class.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add.

If you look at .class file using any java decompiler , you can see that compiler have had replaced object ref variable with type of that variable.
 
Get me the mayor's office! I need to tell her about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic