What is static binding and where it occurs?

Answer Posted / yogesh mudgil

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.

Is This Answer Correct ?    4 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

When the constructor of a class is invoked?

776


Are variables stored in ram?

771


How does compareto work in java?

792


What is an anonymous class in java?

784


What is a byte array?

852


What is a substitution variable?

801


What is the full form of jpeg?

729


Which browsers work with java?

825


How do you trim a space in java?

726


How can you traverse a linked list in java?

900


What’s a deadlock?

849


What does || || mean in math?

705


State the significance of public, private, protected class?

859


What is hashmap in java?

815


What is the purpose of the runtime class in java programming?

797