In java, if a method NOT inherited by any subclass is called, whether dynamic binding or static binding is used?
-
09-02-2021 - |
문제
In java, if a method NOT inherited by any subclass is called, whether dynamic binding or static binding is used?
I know it won't make any difference to the output in this particular case, but just wanted know this.
해결책
Instance method calls in Java always use dynamic binding. Static methods and direct access to private members use static binding.
In length: http://geekexplains.blogspot.com/2008/06/dynamic-binding-vs-static-binding-in.html
This article explains it pretty good.
다른 팁
Unless the class or method is marked final
, it could be overridden by new types introduced at runtime by a class loader. In this sense they are still 'dynamic'.
At runtime, JVM knows all classes loaded, and whether a method is overridden; the final
modifier on methods doesn't matter to JVM.
With that knowledge, JVM will optimize calls to methods that are not overridden; the binding is "static" in that sense.