Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Static Binding and Dynamic Binding..they aren't straight forward are they ?

843785Nov 17 2008 — edited Nov 17 2008
Well i have understood a little about static binding and dynamic binding but still missing a few puzzles.

Static binding happens when we call a static member of a class or an instance method that is private or when i implement method overloading.

Dynamic binding happens when i use Virtual Method Invocation or Override a method.

so,
class A
{
 public void display()
 {
  System.out.println("Display");
 }
}
class B
{
 public static void main(String args[])
 {
  A ref = new A();
  ref.display();
 }
}
Is this static binding ?

Comments

843785
In Java all calls to static method are statically bound (and only those calls).

All calls to non-static methods are dynamically bound in Java.

The code of your example only presents dynamic binding.
788165
Try to think of "binding" as when_ Java makes the connection between the name of your method and the code it executes.
With static binding (early), that association happens when you compile your program.
With dynamic binding (late), that association doesn't happen until the program is actually running.
843785
Is there any reason static binding couldn't be used when a final method is invoked?
843785
final methods cannot be overriden so they must be statically binded. are they ?
843785
NarutoUzumaki wrote:
final methods cannot be overriden so they must be statically binded. are they ?
javap says "invokevirtual"
788165
NarutoUzumaki wrote:
final methods cannot be overriden . . .
But they can be overloaded+.
843785
LazarusLong wrote:
NarutoUzumaki wrote:
final methods cannot be overriden . . .
But they can be overloaded+.
I'm oversexed. Does that count?
788165
DrLaszloJamf wrote:
LazarusLong wrote:
NarutoUzumaki wrote:
final methods cannot be overriden . . .
But they can be overloaded+.
I'm oversexed. Does that count?
I'm not sure how that helps the OP.
Am I missing something?
788165
+" . . . and there's no runtime polymorphism on them."+

Even in the case of method overloading?
843785
NarutoUzumaki wrote:
Well i have understood a little about static binding and dynamic binding but still missing a few puzzles.

Static binding happens when we call a static member of a class or an instance method that is private or when i implement method overloading.

Dynamic binding happens when i use Virtual Method Invocation or Override a method.
Static/dynamic binding refers to WHEN it's decided which method is to be called, at compile time (static binding) or at runtime (dynamic binding).

In principle any method that can be overriden is bound dynamically. This means any non-static, non-final or non-private method is bound dynamically. BUT due to clever optimization at runtime methods that are bound dynamically but aren't overridden are called as if they were statically bound.
843785
jverd wrote:
JoachimSauer wrote:
In Java all calls to static method are statically bound (and only those calls).

All calls to non-static methods are dynamically bound in Java.
I'm fairly certain that final and private methods are also statically bound, since, like static methods, they can't be overridden and there's no runtime polymorphism on them.
Final methods can still be dynamically bound (if they override a non-final method and the non-final method is called)

But other than that, this is the reason why I always have a bad feeling about posting that kind of high-level overview: I feel like I always forget some cases and therefore write illegitimate generalizations.
843785
LazarusLong wrote:
+" . . . and there's no runtime polymorphism on them."+

Even in the case of method overloading?
Overloading is always resolved at compile time (i.e. the exact signature of the method to be called is decided at compile time). Only Overriding is done at runtime (i.e. the exact method to be called from among the methods with the given signature is decided at runtime).
788165
JoachimSauer wrote:
LazarusLong wrote:
+" . . . and there's no runtime polymorphism on them."+

Even in the case of method overloading?
Overloading is always resolved at compile time (i.e. the exact signature of the method to be called is decided at compile time). Only Overriding is done at runtime (i.e. the exact method to be called from among the methods with the given signature is decided at runtime).
Understood.
Thank you.
1 - 13
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Dec 15 2008
Added on Nov 17 2008
16 comments
958 views