Skip to content

Instantly share code, notes, and snippets.

@vaskoz
Created July 9, 2013 05:14
Show Gist options
  • Save vaskoz/5954887 to your computer and use it in GitHub Desktop.
Save vaskoz/5954887 to your computer and use it in GitHub Desktop.
Invokespecial used in 3 cases: call to super method, call to constructor, and call to private methods.
class A {
public void superMethod() {}
}
public class InvokeSpecial extends A {
// Implicit super constructor call - invokespecial
private byte b = 0;
{ // instance initializer - invokespecial
b = 1;
}
public InvokeSpecial() {} // invokespecial
@Override public void superMethod() {
super.superMethod(); // invokespecial
somePrivate(); // invokespecial
new String("foo"); // calling String constructor by invokespecial
}
private void somePrivate() {}
}
//Compiled from "InvokeSpecial.java"
//public class InvokeSpecial extends A {
// public InvokeSpecial();
// Code:
// 0: aload_0
// 1: invokespecial #1 // Method A."<init>":()V
// 4: aload_0
// 5: iconst_0
// 6: putfield #2 // Field b:B
// 9: aload_0
// 10: iconst_1
// 11: putfield #2 // Field b:B
// 14: return
//
// public void superMethod();
// Code:
// 0: aload_0
// 1: invokespecial #3 // Method A.superMethod:()V
// 4: aload_0
// 5: invokespecial #4 // Method somePrivate:()V
// 8: new #5 // class java/lang/String
// 11: dup
// 12: ldc #6 // String foo
// 14: invokespecial #7 // Method java/lang/String."<init>":(Ljava/lang/String;)V
// 17: pop
// 18: return
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment