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".