Lukas Eder
Lukas Eder

Reputation: 220832

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.

Upvotes: 6

Views: 135

Answers (2)

NPE
NPE

Reputation: 500307

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);
}

Upvotes: 6

Robert
Robert

Reputation: 8609

How about

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

Upvotes: 1

Related Questions