Am I following the correct trial ?
You meant to tell, the condition that 'every class is a sub type of Object' only forces the use of dynamic binding during a method call or there exist any other rule.
Fred,please note in my first post "object" refers to object of an arbitrary class not Object(which is the superclass of all java classes)
In C++ the concept of compile time binding exists(subject to correction)
then why 'only dynamic binding' exists in Java?
[ January 24, 2007: Message edited by: vijayk gopu ]
Originally posted by vijayk gopu:
My doubt is whether compile time binding exists in java?
For static methods, yes. But all other Java methods are "virtual" (in the C++ sense), at least in theory.
Remember that Java code is run by a "smart" virtual machine that generally includes some kind of a just-in-time native compiler; this compiler gets a chance to look at the code more closely at runtime and can, indeed, turn some method calls into statically-bound calls, so that no dynamic lookup actually happens. But that's an implementation detail: in theory, every call to a non-static method call is dynamic.
Originally posted by Stan James:
Do final methods get any special treatment? I know the compiler won't let you override them, but don't know if it wires up anything different on the call.
I was thinking about final methods when I wrote the above, and wondered if I should mention them or not. It turns out that for final methods, the Java compiler does nothing special at all; the method call is compiled to the same bytecodes as it would be for a non-final method. So my answer holds.
Now, the bytecode does contain the type of the reference through which the method was called. The truth is that it would be foolish for any VM not to note final methods and classes and use these to turn virtual calls into direct nonvirtual ones. The original VM spec actually described the VM's mechanism for doing something similar with interface method calls, turning them into standard, cheaper virtual calls at runtime. You can bet HotSpot turns virtual calls to final methods into static calls at runtime.