2

I know what is static binding and dynamic binding. So my question is not related to this.

Let's see what static binding has to say according to this:

1) Static binding in Java occurs during Compile time while Dynamic binding occurs during Runtime.

2) private methods, final methods and static methods and variables uses static binding and bonded by compiler while virtual methods are bonded during runtime based upon runtime object.

3) Static binding uses Type(Class in Java) information for binding while Dynamic binding uses Object to resolve binding.

3) Overloaded methods are bonded using static binding while overridden methods are bonded using dynamic binding at runtime.

Let's focus on one aspect of above :

Now we see that static binding binds class and instance variables to their values and static method calls to relevant method body.

But at some places like this , they define binding as

Association of method call to the method body is known as binding.

But binding also binds variables too.

Now I am confused. Is binding related to just method calls to method body or variables to their values too ? How do we define binding ?

Number945
  • 4,631
  • 8
  • 45
  • 83
  • If you already understood the first part (with 4 items, two of them numbered 3?), and it already says that "binding" applies to both methods and variables (you even highlighted that), why is the second quote that just talks about methods confusing you? Just because it says that "Association of method" is called "binding", doesn't mean that other things can't also be called binding. It's not an *exclusive* statement. It doesn't say "binding means method". It says "method is binding". Since the article doesn't talk about variables at all, it simply doesn't cover what that is called. – Andreas Apr 10 '18 at 17:21
  • @Andreas , what I meant is authentic source to where binding is defined. Blogs may use loose terminology too sometimes. How can you be so sure that that the variables to their values are actually called binding only ? – Number945 Apr 10 '18 at 17:25
  • 1
    The "authentic source" for Java is the [Java Language Specification](https://docs.oracle.com/javase/specs/), and it doesn't define "binding". In the entire specification, the word "binding" is used 4 times, and is never defined. [Chapter 15 (Expressions)](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html) says *"This lack of **dynamic lookup for field** accesses allows programs to be run efficiently with straightforward implementations. The power of **late binding** and overriding is available, but **only** when instance **methods** are used."* – Andreas Apr 10 '18 at 17:36
  • You can find general definition of [dynamic binding](https://en.wikipedia.org/wiki/Dynamic_binding_(computing)) and [static binding](https://en.wikipedia.org/wiki/Static_binding) on Wikipedia. – Andreas Apr 10 '18 at 17:39

1 Answers1

2

Most generally, "binding" is about associating an identifier to whatever it identifies, be it a method, a variable, or a type.

All bindings in Java are static ("early") except for bindings of instance methods, which may be static or dynamic ("late"), depending on method's accessibility.

Java Language Specification mentions binding both in the context of accessing fields and in the context of accessing instance methods. Chapter 15.11 compares field binding (static) vs. instance method binding (dynamic), and provides code examples to contrast the two kinds of binding.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523