25

If there are no compile time binding in java, do this mean all binding are done at runtime?

But as per OOPs concept for runtime binding, functions must have virtual keyword..ARE all methods implicitly virtual in java or is there any compile time binding exist in java

If there is any compile time binding, can you give me some specific situation, or links to further information

  1. Static (There is no meaning of binding here as static does not belongs to object)
  2. final (this is not a valid point as it can be achived in another way)
1
  • 1
    "there in no compile time binding in java" - not true. Sure, the implementation is picked at execution time, but overload resolution and the simple act of checking the method exists and is accessible sounds like compile-time binding to me. Perhaps you mean something different by the term "binding" given your final paragraph.
    – Jon Skeet
    Oct 5, 2012 at 19:01

3 Answers 3

43

All non-static, non-final and non-private methods are virtual by default in Java. However JVM is clever enough to find classes having only one implementation of given method and turn it into static binding.

This way you don't have to remember about virtual keyword (ever experienced memory leak due to missing virtual on destructor in C++?) while the performance is not impacted that much.

1
  • 9
    @Arun You don't need to prove that - that's just how the language is defined. If you want, you can look it up in the Java Language Specification.
    – Jesper
    Oct 5, 2012 at 19:33
5

Non-static method invocation is the main (only) dynamic aspect of Java. All methods are virtual in Java. This does not apply to static methods, which are bound at compile time, based on the static type of object.

0

Methods which we can't override in sub class are generally called non virtual methods.

In Java static, private & final methods are non virtual by default. Other methods are virtual by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.