What is binding in Java
Let’s see what static binding has to say according to this: 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. All method binding in Java uses late binding unless the method is static or final. you can find those definitions in section Method-call binding from chapter Polymorphism .
What is binding in Java
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.
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 ?
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.
Difference between One-way Binding and Two-way Binding, In one-way binding, the flow is one-directional. In a two-way binding, the flow is two-directional. This means that the flow of code is from ts file to Html file. This means that the flow of code is from ts file to Html file as well as from Html file to ts file. In order to achieve one-way binding, we used the property …
Method Binding in Java
When I was reading Thinking in Java (4th Edition) recently, I got a problem about method binding in Java. First let’s look at two definitions from the book:
- Connecting a method call to a method body is called binding.
- All method binding in Java uses late binding unless the method is static or final.
you can find those definitions in section Method-call binding from chapter Polymorphism . (page 281-282)
To testify that, I wrote the following code:
public class **** < public static void main(String[] args) < BindingTest_Sub sub1 = new BindingTest_Sub(); BindingTest_Base sub2 = new BindingTest_Sub(); sub1.ovrLd(new Integer(1)); // statement 1 sub2.ovrLd(new Integer(2)); // statement 2 sub2.ovrRd(); // statement 3 >> class BindingTest_Base < void ovrLd(Object obj)< System.out.println("BindingTest_Base ovrLd()"); >void ovrRd() < System.out.println("BindingTest_Base ovrRd()"); >> class BindingTest_Sub extends BindingTest_Base < void ovrLd(Integer i)< System.out.println("BindingTest_Sub ovrLd()"); >void ovrRd() < System.out.println("BindingTest_Sub ovrRd()"); >>
BindingTest_Sub ovrLd() BindingTest_Base ovrLd() BindingTest_Sub ovrRd()
Based on this result, I have following questions:
- According to the definition from the book, since all of my methods are inheritable, Java will use late binding (dynamic binding) for all three statements. However, I read about some other articles, which said that Java use static binding when dealing with overloading. It seems contradictory because obviously statement 1 is overloading.
- I do not fully understand why Java called ovrLd() of base class in statement 2. If it used dynamic binding, it should call overLd() of sub class because by the runtime JVM should be clear that sub2 is an instance of BindingTest_Sub class. On the other hand, if it used static binding, it should also call overLd() of sub class because compiler is able to observe that the type of given argument is an Integer. Can you tell me what job has been done by compiler or JVM when it deals with statement 2.
- The outcome of statement 3 makes sense to me. But still, I am curious about how the compiler recognize it (ovrRd()) as a overriding method. In another words, how does compiler know that there is another class which has a method that overrides this ovrRd().
Any thoughts about above questions or Java method binding mechanism is appreciated. Also please feel free to point out my mistakes.
TL;DR; You’re not really overloading ovrLd(Object) , not in runtime. The compiler uses compile time type information to decide which is the best virtual method to call. sub1 and sub2 have different compile time types. And sub1 ‘s type has a different best match for ovrLb(Integer) .
Explaining. You’re wondering:
- if sub1.ovrLd(new Integer(1)) calls BindingTest_Sub.ovrLd(Integer)
- then why is sub2.ovrLd(new Integer(2)) is calling BindingTest_Base.ovrLd(Object)
In this case it works like this:
The compiler uses the compile time type information of the variable to decide which method to call. The compile time type of sub2 is BindingTest_Base , in runtime you are assigning a BindingTest_Sub to it, but that’s not relevant to the compiler.
The only method from BindingTest_Base that matches the parameters of that call is: BindingTest_Base.ovrLd(Object)
So the compiler issues a virtual call to orvLd(Object)
Now, the runtime method for a virtual call is decided based on the full signature of the method being invoked (name + parameters). And there is no overload for ovrLd(Object) in BindingTest_Sub So the base class method is invoked.
With sub1 the compiler has more information. sub1 ‘s compile time type is BintdingTest_Sub and there is a method that matches ovrLd(Integer) in that class.
Looking at the bytecode you can see this clearly:
aload 1 // sub1 // . blah blah blah creating the integer // the last opcode issued by the compiler for "statement 1" INVOKEVIRTUAL com/ea/orbit/actors/samples/helloworld/BindingTest_Sub.ovrLd (Ljava/lang/Integer;)V aload 2 // sub2 // . blah blah blah creating the integer // the last opcode issued by the compiler for "statement 2" INVOKEVIRTUAL com/ea/orbit/actors/samples/helloworld/BindingTest_Base.ovrLd (Ljava/lang/Object;)V
After doing some research, I think I understand what the author of Thinking in Java tried to convey.
The author said that All method binding in Java uses late binding unless the method is static or final.
I think this is true, but ambiguous. The ambiguity is from the term late binding . From my understanding, the binding here means the determination of specific implementation of method, not resolution of method (resolved to a symbol in symbol table). In other words, the compiler just refers a method to a symbol, but where in memory does that symbol points to is undetermined.
At the point of class loading, static method and final method ( private method are implicitly final ) are associated with real memory address of the method (specific implementation). Because those method can not be overridden or even accessed by others, their implementation can not be changed dynamically. Except for those methods, other methods are bound to specific implementation at run time.
Is UI data binding in Java more trouble than it is worth?, I’ve spent some time recently learning and attempting to use various Java data-binding tools such as JGoodies, GlazedLists, JSR-295, etc. The problems that I’ve been trying to solve are not that difficult, however the amount of code I’ve had to write in support of the binding process heavily outweighs any …
Late Binding in Java
I have searched through all the similar questions on late binding on stack overflow, and I would severely disagree with anyone who marks this question as a duplicate. First off, i found this example on another question, but I do not understand how I am supposed to know when something is decided during compile time and when something is decided during run time. Basically, the crux of my question boils down to two things:
- What in this example must bring me to the logical conclusion that one method is late binding and another is early binding
- How do I know when the decision about which version of a method to execute is decided during run-time or compile time in Java
class A < public void foo() < System.out.println("Class A"); >> class B extends A < public void foo() < System.out.println("Class B"); >> public class C < public static void main(String [] args) < A a=new A(); B b=new B(); A ref=null; /* early binding --- calls method foo() of class A and decided at compile time */ a.foo(); /* early binding --- calls method foo() of class B and decided at compile time */ b.foo(); /* late binding --- --- calls method foo() of class B and decided at Run time */ ref=b; ref.foo(); >>
Wrong on all counts. The method to be called is decided at runtime, in every case here, based on the runtime type of the object. The only decisions made at compile time are for calls to final, private, or static methods, or choices among a set of overloaded methods (which could still lead to runtime choices if the overloaded methods are not final, private, or static.)
Java uses late binding for all non-final, non-private instance methods. This is how polymorphism is implemented. All of the calls you commented on are determined at run time.
a is referencing an A object so A ‘s implementation will be found, bound and used.
b is referencing a B object so B ‘s implementation will be found, bound, and used.
ref is referencing a B object so B ‘s implementation will be found, bound, and used.
The static (declared) type of the variable is only used by the compiler to verify that such a method is accessible on that type.
All the answers here are mostly correct, but there is one key point missing regarding late binding in Java.
Java does not perform «by the book» late binding if we go by the definition of late binding. Late binding in its book definition form means that the compiler should perform no argument checks, no type checks on a method call and should leave it all to the runtime — because possibly the compiler does not have access to the method implementation code (for example in COM programming). Java however at compile time does verify, even in a polymorphic scenario, that the method being called and the method signature does exist somewhere in the type hierarchy of the expression that qualifies the method. So for example, lets say I call a method foo1 on ref that does not exist in either A or B:
A ref=null; ref=new B(); ref.foo1(); //This will not compile in Java, because java will check at compile time //for the method foo1 in the type hierarchy of A, which is the type of the // variable ref at compile time. //In pure late binding, however this would pass compilation and //throw an error at runtime.
In a pure late binding scenario, the determination of whether the method foo1() exists or not with the right number of arguments is made purely at runtime . However in Java, there is some level of checking done at compile time, to ensure that the method with the right number of arguments does exist somewhere in the type hierarchy.
The only time that I think Java does perform pure late binding is if one uses reflection to call a method. What Java does is better termed as dynamic dispatch rather than late binding, but everyone calls it late binding in Java and hence there is confusion.
A ref; //reference of A ref = new B();//Object of B ref.f2();
Here ref is a reference of class A and it has address of object of class B f2() is a overridden method.
When compiler detects such a statement then it doesn’t bind the function call with any definition. It only validates the call.
Binding of such calls is left for the runtime environment. At program runtime system identifies the datatype of the object and binds the function call with the function definition provided by the class of object. This type of binding between the function call and function defination is called as «late binding» or » runtime binding » or «runtime polymorphism» or » dynamic method dispatch».
go through this question and read my answer example is also given there.
How dynamic binding works in java?, Introduction to Dynamic Binding in Java “Dynamic” means “run time”, and “binding” means “association”. So the term dynamic binding indicates run time association of objects by java virtual machine.Here we will see how Java achieves dynamic binding in run time, which means before the code’s final running but after …