0

I have a simple question about dynamic vs static. So from what I've read, static binding is for private, final, overloaded, static methods and it relies on the type of reference. Dynamic is for instance methods not private, final, overloaded and relies on the type of the object.

If you do something like Human myobj = new Boy();, which is the type of reference and which is the type of the object?

Umar Farooq
  • 321
  • 1
  • 9
  • 2
    So ... you recognize that static and dynamic binding apply to *methods*, but then asking something about objects and references? – Andy Thomas Nov 06 '15 at 19:45
  • I'm not an expert, but I believe your example is static as well. All information is available at compile time, so the compiler should be able to preform all necessary checks. The type of the reference is `Human` and the type of the object is `Boy`, just like it says on the tin. – markspace Nov 06 '15 at 20:25

2 Answers2

2

Your statement is incorrect about final and overloaded. Don't know where you read that.

Compiling the following code using Java 8 (1.8.0_51).

public class Test {
    public static void main(String[] args) {
        Test x = new Test();
        x.a();
        x.b();
        x.c();
        x.d(1);
        x.d(1L);
        x.d(1d);
        x.d(null);
    }
    private       void a() {}
    public final  void b() {}
    public static void c() {}
    private       void d(int x) {}
    public final  void d(long x) {}
    public static void d(double x) {}
    public        void d(String x) {}
}

Note: Calling static methods using instance variable is bad form. Should be done using class name, and is therefore explicitly static. Code is using instance variable for illustration purpose only.

Decompiling the byte code show:

   0: new           #1                  // class test/Test
   3: dup
   4: invokespecial #19                 // Method "<init>":()V
   7: astore_1
   8: aload_1
   9: invokespecial #20                 // Method a:()V
  12: aload_1
  13: invokevirtual #23                 // Method b:()V
  16: invokestatic  #26                 // Method c:()V
  19: aload_1
  20: iconst_1
  21: invokespecial #29                 // Method d:(I)V
  24: aload_1
  25: lconst_1
  26: invokevirtual #33                 // Method d:(J)V
  29: dconst_1
  30: invokestatic  #36                 // Method d:(D)V
  33: aload_1
  34: aconst_null
  35: invokevirtual #39                 // Method d:(Ljava/lang/String;)V
  38: return

The private methods a() and d(int) are invoked specially.
The static methods c() and d(double) are invoked statically.
The remaining methods b(), d(long), and d(String) are invoked virtually.

As you can see, final and overloading doesn't affect the result.

From the documentation of invokestatic:

Invoke a class (static) method

From the documentation of invokespecial:

Invoke instance method; special handling for superclass, private, and instance initialization method invocations

From the documentation of invokevirtual:

Invoke instance method; dispatch based on class

"Dispatch based" means dynamically bound, the other two are statically bound.

There are two more invoke instructions:

  • invokeinterface: Like invokevirtual but on an interface reference, instead of a class reference.
  • invokedynamic: Mostly used for dynamic (scripted) languages such a Groovy and "is invoked as if by execution of an invokevirtual instruction".
Andreas
  • 154,647
  • 11
  • 152
  • 247
1

Static binding in Java occurs during Compile time while Dynamic binding occurs during Runtime, so your type is Human and the type of the object (if you do a instanceof for example) will be Boy.

Francisco Hernandez
  • 2,378
  • 14
  • 18