Search code examples
javainner-classesambiguous

How to access a member of a nested class, that is hidden by a member of the outer class


I have a source-code generator that risks generating the following type of code (just an example):

public class Outer {
    public static final Object Inner = new Object();

    public static class Inner {
        public static final Object Help = new Object();
    }

    public static void main(String[] args) {
        System.out.println(Outer.Inner.Help);
        //                             ^^^^ Cannot access Help
    }
}

In the above example, Inner is ambiguously defined inside of Outer. Outer.Inner can be both a nested class, and a static member. It seems as though both javac and Eclipse compilers cannot dereference Outer.Inner.Help. How can I access Help?

Remember, the above code is generated, so renaming things is not a (simple) option.


Solution

  • The following works for me (with a warning about accessing static members in a non-static way):

    public static void main(String[] args) {
        System.out.println(((Inner)null).Help);
    }