java - Is it true to say that every run time binding was a static binding during compilation?
问题描述
I'm pretty confused about the topic binding. As we know in static binding, the type of reference is checked to determine the binding, not the object it is referencing, and in dynamic binding, the type of object the reference is pointing is taken into consideration.
class A
{
void show()
{
System.out.println("From A");
}
}
class B extends A
{
void show()
{
System.out.println("From B");
}
}
class Main
{
public static void main(String[] quora)
{
A a1 = new A();
A a2 = new B();
a1.show(); //line 1
a2.show(); //line 2
}
}
In the above example, we can see during compile time, both Line1 and line2 will be treated by static binding and they will link to the A-class method show( since the type of reference is A). But during runtime, the call is resolved and line1 links to the A-class method show() and line2 links to the B-class method i.e, type of object or we can say dynamic binding.
So my main intention is to know the following.
Is dynamic binding always results after static binding? or I'm understanding something wrong?
If it's true then is it true to say every method is dynamically linked during the run time?
Can we generalise it?
解决方案
Basically, every time you call a method, the compiler has to figure out which overload to call, based on the types and the arguments you pass to the method. This is static binding. Then at runtime, the runtime will figure out which implementation of the overload (which is already decided at this point) to call.
Consider:
class A
{
void f(Object o)
{
System.out.println("From A");
}
}
class B extends A
{
void f(Object o)
{
System.out.println("From B (Object)");
}
void f(String s)
{
System.out.println("From B (String)");
}
}
And you do:
A a = new B();
a.f("");
What happens? At compile time, the compiler has chosen the overload f(Object)
due to the type of a
being A
(there is only one method to choose from!). Then at runtime, since a
actually refers to an instance of B
, B
's implementation of the f(Object)
overload gets called, printing From B (Object)
. One misconception I've seen is to expect the output to be From B (String)
. This is wrong because the compiler doesn't know about the existence of this overload on a
.
Is dynamic binding always results after static binding?
As we can see from the results of the above code, the answer is yes. Static binding happens at compile time, dynamic binding happens at runtime, so the former always happens first.
If it's true then is it true to say every method is dynamically linked during the run time?
I'm not sure what you mean by this, so hopefully the detailed explanation above would have answered this already.
no matter what, there's always gonna be static binding and then dynamic binding for every method call?
Yes, but sometimes these two processes can go very quickly though, because there is very few choices. You probably don't need to make a "choice" in dynamic binding when the class you are calling the method on is a final class, do you?
推荐阅读
- javascript - 在 React 中单击轮播图像时如何重定向?
- flutter - 在 Flutter 中,如何使用 DateTime 获取不同时区的时间?
- .net - AWS - Elastic Beanstalk - eb 部署在 Linux 上的 .NET 核心上失败
- kubernetes - 是否可以防止 k8s 秘密为空(零字节)?
- javascript - 使用 Axios 从文件中检索数据,html 文件返回 404 错误但其他文件类型工作
- php - 更新时是否必须绑定数据库表中的所有值,并且更新类似于插入?
- file - MiNiFi GetFile 处理器无法获取大文件
- spring-boot - 发送带有 key='null' 的消息时,Kafka 字符串消息抛出异常
- javascript - 如何删除 V-autocomplete vuetify 中的选择?
- javascript - Mongodb为字段设置最大值?