内部类和 Lambda 表达式的命名规则
1) 成员内部类,包括普通成员内部类、静态成员内部类,外部类名$内部类名
2) 局部内部类,外部类名$n内部类名
,n 从 1 开始,每个函数都有不同的 n 值
3) 匿名内部类,外部类名$n
,n 从 1 开始
4) Lambda 表达式类,外部类名$$Lambda$n
,n 从 1 开始。
(注:控制台输出后面还会加上斜杠+数字,比如:Main$$Lambda$1/1324119927,大致就是为了生成不重复的接口。
Main$$Lambda$1.class是使用 ASM 字节码技术动态生成的,默认情况下看不到而已。)
5) Lambda 表达式方法,lambda$方法名$n
,n 从 0 开始,比如lambda$main$0
不同于 C/C++,在 Java 中,
$
美元符也是一个合法的标识符。
注:上述类也不一定必须从1开始:
The JLS 13.1 specifies:
The class or interface must be named by its binary name, which must meet the following constraints:
- The binary name of a top level type (§7.6)is its canonical name (§6.7).
- [...]
- The binary name of a local class (§14.3) consists of the binary name of its immediately enclosing type, followed by $, followed by a non-empty sequence of digits, followed by the simple name of the local class.
- The binary name of an anonymous class (§15.9.5)consists of the binary name of its immediately enclosing type, followed by $, followed by a non-empty sequence of digits.
版权声明:本文为a3192048原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。