Take the 2-minute tour ×

I"m a little confused on how private methods can be static and but public must be dynamically bound.

share|improve this question

closed as not constructive by Nambari, prolink007, oers, Blue Moon, ρяσѕρєя K Jul 20 '12 at 15:46


Your question is not clear (at-least for me), can you add more details? –  Nambari Jul 19 '12 at 17:46
The modifiers private, public and protected imply visibility. They have to do nothing with binding. What do you mean by your question? –  Lion Jul 19 '12 at 17:51
I agree with thinksteep. When posting questions like this it is best practice to include at least one example code snippet of each case (here you would have 3 snippets, what you are trying to do and doesn't work, the static case, and dynamic case). The more examples and details you provide, the better quality answer you will receive. –  Crackers Jul 19 '12 at 17:54
this is for a class I'm taking and that is basically the question there is not code snippet –  user1538694 Jul 19 '12 at 18:30

5 Answers 5

Dynamic binding means that the decision as to which code is run is made at runtime. This is the basis of polymorphism.

Public, package access and protected methods are dynamically bound. Subclasses can override the methods and provide alternative implementations. Private methods cannot be overridden, so dynamic binding is not needed for them.

Static methods are not dynamically bound (the clue's in the name) as they are defined on the class itself rather than being unique to each object.

share|improve this answer
+1 This is correct answer. Dynamism is bounded at runtime and static methods HIDE the implementation. –  Nambari Jul 19 '12 at 18:02
Where did you get that "package access methods cannot be overridden"? That it patently false. –  Marko Topolnik Jul 19 '12 at 18:31
It was a mistake. Thanks for updating. –  aetheria Jul 19 '12 at 19:50

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.


Static Binding or Early Binding



[Apart from this discussion, you can not override static (as mentioned by the above link), private and final methods in Java.]

share|improve this answer

static methods are class level methods so u can accessed through the class name only but methods must be public

share|improve this answer
Static methods don't have to be public, but if they're private, they can only be accessed by other static methods of the same class... –  Simon MᶜKenzie Jul 20 '12 at 4:14

1. In Class polymorphism, the Object Reference Variable of the Subclass is passed as an Argument to the method which accepts the Parameter which is an Object Reference Variable of type Superclass.Public and Protected and Default members can be dynamically bounded, Private members are not are not inherited.

2. Static members are for the class, and its not bounded to any individual object, so its not dynamically bounded.

share|improve this answer

If you don't need the this reference inside a private method, you can just as well make it static and it won't make any difference. A private method will never need to be dynamically bound in either case so at the implementation level it is really irrelevant which you choose. It helps readabilty, though, when you mark the method static since then it is clear the method does not depend on the state of the object.

share|improve this answer
