I have the following class containing local inner class:
class Outer {
private boolean beep;
private int foo;
public Outer(boolean beep) {
this.beep = beep;
}
public void start(boolean beep) {
class LocalInner {
private LocalInner() {
}
public void method() {
System.out.println("Test.");
if (beep) {
System.out.println("beeping.");
}
}
}
LocalInner li = new LocalInner();
li.method();
}
}
When I compile the class javac Outer.class
, and then check compiled members of Outer$1LocalInner
with javap -private Outer\$1LocalClass.class
, I get this:
class Outer$1LocalInner {
final boolean val$beep;
final Outer this$0;
Outer$1LocalInner();
public void method();
}
I was expecting that the constructor will be compiled to: Outer$1LocalInner(Outer, boolean)
. When I tried to have a look at the byte code javap -c -s -private Outer$1LocalInner.class
I got this:
class Outer$1LocalInner {
final boolean val$beep;
descriptor: Z
final Outer this$0;
descriptor: LOuter;
Outer$1LocalInner();
descriptor: (LOuter;Z)V
Code:
0: aload_0
1: aload_1
2: putfield #1 // Field this$0:LOuter;
5: aload_0
6: iload_2
7: putfield #2 // Field val$beep:Z
10: aload_0
11: invokespecial #3 // Method java/lang/Object."<init>":()V
14: return
...
}
Now this is rather interesting! Let's have a closer look at these two lines:
Outer$1LocalInner();
descriptor: (LOuter;Z)V
Why is there no parameters to
Outer$1LocalInner()
constructor, yet I can see in the method descriptor that it does accept two parameters as I'm expectingOuter
andboolean
?Why does the compiler ignores the access modifier of the local inner class? I'm declaring it as private, yet the disassembled version is package modifier.