Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static/dynamic binding and static/dynamic dispatch are a bit confusing

Tags:

java

I would like to clarify a few things about binding and dispatch.

Binding is attaching a method call to a method signature. Dispatch is choosing over method implementations. First comes binding, then comes dispatch. OK, I got this, but:

  1. Do instance methods that have not been overridden use static binding?
  2. Why exactly do overloaded methods use static binding since they are virtual and can be overridden in subclasses?

I've read lots of explanations over the last few days and it's a mess for me now. Some of them are conflicting or completely opposite from one another. From what I know all non-static, non-private and non-final methods in Java are virtual by default and must use dynamic binding and dynamic dispatch.

like image 391
violet_eyes Avatar asked Dec 08 '25 22:12

violet_eyes


1 Answers

Binding is attaching a method call to a method signature. Dispatch is choosing over method implementations.

Binding in the sense you mean refers to overloading. Dispatch in the sense you mean refers to overriding.

First comes binding, then comes dispatch.

Yes. The compiler does the binding, the JVM does dispatching.

OK, I got this, but:

  1. Do instance methods that have not been overridden use static binding?

No. The compiler can't tell that, except in the case of private methods, in which case it uses the invokespecial bytecode.

  1. Why exactly do overloaded methods use static binding since they are virtual and can be overridden in subclasses?

They don't.

From what I know all non-static, non-private and non-final methods in Java are virtual by default and must use dynamic binding and dynamic dispatch.

Correct.

like image 155
user207421 Avatar answered Dec 10 '25 12:12

user207421