Java class name containing dollar sign fails to compile if an inner class is present

后端 未结 3 1220
野趣味
野趣味 2020-11-29 03:21

I\'ve got following Java classes defined:

mac-grek:javajunk grek$ cat A\\$B.java
class A$B {}
mac-grek:javajunk grek$ cat A.java
public class A {
  public st         


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 04:13

    You have a name conflict because you defined a top-level class A$B having the same name as the generated name for a static inner class B of class A. Since you have both, the compiler can't resolve the conflict.

    The JLS says:

    The $ character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

    Since you decided not to respect that rule, you got bitten by javac. I would just rename A$B to something else.

提交回复
热议问题