为什么javac会创建一个额外的类? [英] Why does javac create an additional class?
问题描述
我编译了以下代码(为简洁起见,省略了方法和变量):
I have compiled the following code (Methods and variables are elided for brevity):
// Outer.java
public class Outer
{
private class Inner
{
}
void someMethod()
{
Inner inObj = this.new Inner();
}
public static void main(String s[])
{
Outer outerObj = new Outer();
}
}
当我列出创建的类时,它显示以下内容:
When I listed the classes created, it displayed the following:
Outer$1.class
Outer$Inner.class
Outer.class
外部和外部内部显得合乎逻辑。 外部$ 1 课程的目的是什么?在时间尺度上创建这些的顺序是什么?
Outer and Outer$Inner appear logical. What is the purpose of Outer$1 class? What is the order of creation of these in time scale?
推荐答案
好奇。我不确定这是为了什么。但是如果您对类进行反编译,则可以看到如何使用它:
Curious. I'm not sure what this is for. But if you decompile the classes, you can see how it is used:
public class Outer {
public Outer();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
void someMethod();
Code:
0: new #2 // class Outer$Inner
3: dup
4: aload_0
5: aconst_null
6: invokespecial #3 // Method Outer$Inner."<init>":(LOuter;LOuter$1;)V
9: astore_1
10: return
public static void main(java.lang.String[]);
Code:
0: new #4 // class Outer
3: dup
4: invokespecial #5 // Method "<init>":()V
7: astore_1
8: return
}
class Outer$Inner {
final Outer this$0;
Outer$Inner(Outer, Outer$1);
Code:
0: aload_0
1: aload_1
2: invokespecial #1 // Method "<init>":(LOuter;)V
5: return
}
class Outer$1 {
}
所以,外部$ 1
似乎什么也没包含 - 甚至不是默认的构造函数。但是对外部$ 1
的引用必须传递给外部$ Inner
来构建它。神奇的是, someMethod
中传递的值是 null
( someMethod $ c中的第5行$ c>)。
So, Outer$1
seems to contain nothing - not even a default constructor. But a reference to a Outer$1
has to be passed to Outer$Inner
to construct it. Mysteriously, the value passed in someMethod
is null
(line 5 in someMethod
).
这篇关于为什么javac会创建一个额外的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!