Java语言规范中的嵌套嵌套类名称 [英] Mangled nested class namein the Java Language Specification

查看:92
本文介绍了Java语言规范中的嵌套嵌套类名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下两个类:

// a.java
public class a
{
    public static class $b
    {

    }
}


// a$.java
public class a$
{
    public static class b
    {

    }
}

很显然,由于内部/嵌套类名的混乱,a $ .b和a.$ b都将被编译为一个名为a $$ b.class的类文件.执行命令javac a.java a$.java时,Oracle Java编译器(javac 1.7.0_45)产生以下输出:

Obviously, due to inner/nested class name mangling, a$.b and a.$b will both be compiled to a class file named a$$b.class. When the command javac a.java a$.java is executed, the Oracle Java compiler (javac 1.7.0_45) produces the following output:

a$.java:3: error: duplicate class: a.$b
    public static class b
                  ^
1 error

在Java语言规范中,这些类名(a$.ba.$b)在哪里说冲突,或者由于输出文件具有相同的名称,这只是已建立的约定吗?

Where does it say in the Java Language Specification that these class names (a$.b and a.$b ) clash, or is this just an established convention due to the output files having the same name?

推荐答案

标识符是Java字母和Java数字的无限长度序列,其中第一个必须是Java字母.

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.

...

"Java字母"包括大写和小写ASCII拉丁字母AZ(\ u0041- \ u005a)和az(\ u0061- \ u007a),并且由于历史原因,ASCII下划线(_或\ u005f)和美元符号($或\ u0024). 仅在机械生成的源代码中使用$字符,或者很少使用$字符来访问旧版系统上的现有名称.

The "Java letters" include uppercase and lowercase ASCII Latin letters A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

(重点是我的)

尽管实际上,这只是在生成字节码后拥有重复的类名的问题.如果两个冲突的类名在源中不涉及$,则会发生相同的错误.

Though really, it's just a matter of you having a duplicate class name after bytecode generation. The same error would occur with two colliding class names that didn't involve $ in the source.

编辑以添加:二进制格式在 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆