0

This question might sound stupid to some, but I need to get it clear in my mind.

 class J_SuperClass { 



void mb_method() { 
     System.out.println("J_SuperClass::mb_method"); 
   } 
   static void mb_methodStatic() { 
      System.out.println("J_SuperClass::mb_methodStatic"); 
   } 
} 
public class J_Test extends J_SuperClass { 
   void mb_method() { 
     System.out.println("J_Test::mb_method"); 
   } 
   static void mb_methodStatic() { 
     System.out.println("J_Test::mb_methodStatic"); 
   } 
   public static void main(String[] args) { 
     J_SuperClass a = new J_Test(); 
     a.mb_method(); 
     a.mb_methodStatic(); 
     J_Test b = new J_Test(); 
     b.mb_method();
     b.mb_methodStatic(); 
   } 
}

Output is:

J_Test::mb_method 
J_SuperClass::mb_methodStatic 
J_Test::mb_method 
J_Test::mb_methodStatic

I know that dynamic binding occurs at runtime and static binding occurs at compile time. Also, for dynamic binding, the object's actual type determines which method is invoked. So my question is that in the above code, the word "static" causes static binding and hence the object's DECLARED type determines which method is invoked?

coffeeak
  • 2,980
  • 7
  • 44
  • 87
  • @user1109363 Re : your salting question, have a look [here](http://stackoverflow.com/questions/12724935/salt-and-passwords) and [here](http://stackoverflow.com/questions/420843/how-does-password-salt-help-against-a-rainbow-table-attack) – StuartLC Nov 22 '12 at 07:41

2 Answers2

1

Yes, the word static determines static binding because there is no polymorphism (hence need for dynamic binding) in the case of static methods. The method is bound to the name, not to the type since it is the only one with that name in that class; it is not related to any object since it's static.

Random42
  • 8,989
  • 6
  • 55
  • 86
0

You may try decompiling J_Test class. There you'll see:

public static void main(String args[])
    {
        J_SuperClass a = new J_Test();
        a.mb_method();
        J_SuperClass.mb_methodStatic();
        J_Test b = new J_Test();
        b.mb_method();
        mb_methodStatic();
    }

So, the static method of the super class is bound.

hcg
  • 642
  • 5
  • 8