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 ambiguously defined inside Outer . Outer.Inner can be either a nested class or a static member. It seems that both javac and Eclipse compilers cannot dereference Outer.Inner.Help . How can I access Help ?

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

+6
source share
2 answers

The following works for me (with a warning about access to static members non-statically):

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

What about

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

Source: https://habr.com/ru/post/916831/


All Articles