似乎javac为匿名内部类生成$ n个文件,但哪个内部类被编译为$ 1.class。是否有任何优先级,javac将内部类编译为$ 1.class文件或是随机的?
public static void main(String args[]) {
Thread secondThread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Printing from second thread");
}
});
Thread firstThread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Printing from first thread");
}
});
firstThread.start();
secondThread.start();
}
我发现firstThread成为Test $ 1.class(我使用javap -c
来检查)所以我猜测它不是根据首先定义的内部类。那么创作的标准是什么?
javap -c
输出
Compiled from "Test.java"
class main.Test$1 implements java.lang.Runnable {
main.Test$1();
Code:
0: aload_0
1: invokespecial #10 // Method java/lang/Object."<init>":()V
4: return
public void run();
Code:
0: getstatic #17 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #23 // String Printing from first thread
5: invokevirtual #25 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}
Compiled from "Test.java"
class main.Test$2 implements java.lang.Runnable {
main.Test$2();
Code:
0: aload_0
1: invokespecial #10 // Method java/lang/Object."<init>":()V
4: return
public void run();
Code:
0: getstatic #17 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #23 // String Printing from second thread
5: invokevirtual #25 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}
上面你看到包含firstThread的Test $ 1,即使firstThread位于main()
的稍后位置,这有什么原因吗?