This discussion is archived
4 Replies Latest reply: Nov 10, 2008 3:44 PM by 843785 RSS

what is static binding and dynamic biding?

843785 Newbie
Currently Being Moderated
hi

can any one tell me what is static binding and dynamic biding?

my asumtion is static biding is a method overloading and dynamic binding is a method overriding.. i dont know this is correct or not ..can any one help me..


thanks in advance
  • 1. Re: what is static binding and dynamic biding?
    843785 Newbie
    Currently Being Moderated
    Hii..i hope this wil help u..

    Dynamic Binding or Late Binding

    Dynamic Binding refers to the case where compiler is not able to resolve the call and the binding is done at runtime only. Let's try to understand this. Suppose we have a class named 'SuperClass' and another class named 'SubClass' extends it. Now a 'SuperClass' reference can be assigned to an object of the type 'SubClass' as well. If we have a method (say 'someMethod()') in the 'SuperClass' which we override in the 'SubClass' then a call of that method on a 'SuperClass' reference can only be resolved at runtime as the compiler can't be sure of what type of object this reference would be pointing to at runtime.

    ...
    SuperClass superClass1 = new SuperClass();
    SuperClass superClass2 = new SubClass();
    ...

    superClass1.someMethod(); // SuperClass version is called
    superClass2.someMethod(); // SubClass version is called
    ....

    Here, we see that even though both the object references superClass1 and superClass2 are of type 'SuperClass' only, but at run time they refer to the objects of types 'SuperClass' and 'SubClass' respectively.

    Hence, at compile time the compiler can't be sure if the call to the method 'someMethod()' on these references actually refer to which version of the method - the super class version or the sub class version.

    Thus, we see that dynamic binding in Java simply binds the method calls (inherited methods only as they can be overriden in a sub class and hence compiler may not be sure of which version of the method to call) based on the actual object type and not on the declared type of the object reference.

    Static Binding or Early Binding

    If the compiler can resolve the binding at the compile time only then such a binding is called Static Binding or Early Binding. All the instance method calls are always resolved at runtime, but all the static method calls are resolved at compile time itself and hence we have static binding for static method calls. Because static methods are class methods and hence they can be accessed using the class name itself (in fact they are encourgaed to be used using their corresponding class names only and not by using the object references) and therefore access to them is required to be resolved during compile time only using the compile time type information. That's the reason why static methods can not actually be overriden.

    Similarly, access to all the member variables in Java follows static binding as Java doesn't support (in fact, it discourages) polymorphic behavior of member variables. For example:-

    class SuperClass{
    ...
    public String someVariable = "Some Variable in SuperClass";
    ...
    }

    class SubClass extends SuperClass{
    ...
    public String someVariable = "Some Variable in SubClass";
    ...
    }
    ...
    ...

    SuperClass superClass1 = new SuperClass();
    SuperClass superClass2 = new SubClass();

    System.out.println(superClass1.someVariable);
    System.out.println(superClass2.someVariable);
    ...

    Output:-
    Some Variable in SuperClass
    Some Variable in SuperClass

    We can observe that in both the cases, the member variable is resolved based on the declared type of the object reference only, which the compiler is capable of finding as early as at the compile time only and hence a static binding in this case. Another example of static binding is that of 'private' methods as they are never inherited and the compile can resolve calls to any private method at compile time only.
  • 2. Re: what is static binding and dynamic biding?
    843785 Newbie
    Currently Being Moderated
    Revathy.S, please stop encouraging this endless dumping of homework questions on the forum
  • 3. Re: what is static binding and dynamic biding?
    843785 Newbie
    Currently Being Moderated
    georgemc wrote:
    Revathy.S, please stop encouraging this endless dumping of homework questions on the forum
    Additionally, @Revathy.S, copy-pasting such things is bad form if you don't cite [the source|http://geekexplains.blogspot.com/2008/06/dynamic-binding-vs-static-binding-in.html] too.

    Google is your friend, but it's mine too :) [See for yourself!|http://www.google.ca/search?q=If+the+compiler+can+resolve+the+binding+at+the+compile+time&ie=utf-8&oe=utf-8]
  • 4. Re: what is static binding and dynamic biding?
    843785 Newbie
    Currently Being Moderated
    I see maha_the_b00b still hasn't learnt how to do their own research.