如何完全限定其包名称与本地成员名称冲突的类?

问题描述:

好的,这是供JLS专家使用的一个非常好奇的Java 7语言难题.下列代码均无法使用javac或Eclipse进行编译:

OK, here's a very curious Java 7 language puzzle for the JLS specialists out there. The following piece of code won't compile, neither with javac nor with Eclipse:

package com.example;

public class X {
    public static X com = new X();

    public void x() {
        System.out.println(com.example.X.com);
        // cannot find symbol  ^^^^^^^
    }
}

似乎成员com完全禁止从X内部访问com.*软件包.但是,这还没有完全应用.例如,下面的作品:

It appears as though the member com completely prevents access to the com.* packages from within X. This isn't thoroughly applied, however. The following works, for instance:

public void x() {
    System.out.println(com.example.X.class);
}

我的问题:

  • JLS如何证明这种行为合理?
  • 如何解决此问题

请注意,这只是对生成的代码中的实际问题的简化,其中需要完全限定com.example.X并且com成员不能重命名.

Note, this is just a simplification for a real problem in generated code, where full qualification of com.example.X is needed and the com member cannot be renamed.

更新:我认为这实际上可能是类似的问题:为什么我不能静态导入" 等于" Java中的方法?

Update: I think it may actually be a similar problem like this one: Why can't I "static import" an "equals" method in Java?