Skip to content

Instantly share code, notes, and snippets.

@passy
Created January 14, 2016 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save passy/97f77a7c1da8a68180f1 to your computer and use it in GitHub Desktop.
Save passy/97f77a7c1da8a68180f1 to your computer and use it in GitHub Desktop.
public class TestClass {
public static void main(String... args) {
new Inner();
}
// Private class, package-level constructor.
private static class Inner {
Inner() {
System.out.println("Hello, world.");
}
}
}
Compiled from "TestClass.java"
class TestClass$Inner {
TestClass$Inner();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
7: ldc #3 // String Hello, world.
9: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
12: return
}
Compiled from "TestClass.java"
public class TestClass {
public TestClass();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String...);
Code:
0: new #2 // class TestClass$Inner
3: dup
4: invokespecial #3 // Method TestClass$Inner."<init>":()V
7: pop
8: return
}
public class TestClass {
public static void main(String... args) {
new Inner();
}
static class Inner {
private Inner() {
System.out.println("Hello, world.");
}
}
}
Compiled from "TestClass.java"
class TestClass$1 {
}
Compiled from "TestClass.java"
class TestClass$Inner {
private TestClass$Inner();
Code:
0: aload_0
1: invokespecial #2 // Method java/lang/Object."<init>":()V
4: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
7: ldc #4 // String Hello, world.
9: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
12: return
TestClass$Inner(TestClass$1);
Code:
0: aload_0
1: invokespecial #1 // Method "<init>":()V
4: return
}
Compiled from "TestClass.java"
public class TestClass {
public TestClass();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String...);
Code:
0: new #2 // class TestClass$Inner
3: dup
4: aconst_null
5: invokespecial #3 // Method TestClass$Inner."<init>":(LTestClass$1;)V
8: pop
9: return
}
@passy
Copy link
Author

passy commented Jan 14, 2016

Private inner classes don't add additional overhead in terms of methods or class files, whereas private constructors infamously do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment