Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
@passy

passy Jan 14, 2016

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

Owner

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