0
 public static class Outer {
        public int field;
        public class Inner {}
 }


 //  caller methods
 public static void foo(Outer.Inner inner) {
       // here I want to access the "field"
       // tried the following, none worked
       System.out.println(inner.Outer.field);
       System.out.println(inner.Outer.this.field);
 }

How do I access a non-static fields of a class from a reference of its non-static inner class in Java?

P.S
People kept saying it's bad design. Yes, I agree 100% this is bad "design". At least, it's bad for code that needs to be read by humans. But if this is for generated code, I think I'll get a pass. (have anyone tried reading code that comes out of Antlr, for eg?)

One Two Three
  • 22,327
  • 24
  • 73
  • 114
  • 1
    Not a Java expert but that seems like an invalid thing to do. Why do you want to do this anyways, kind of sounds like an XY problem. – Andrew Li Dec 15 '17 at 23:02
  • Nope you cant do that. You would have to add either getters to Outer or Inner – Antoniossss Dec 15 '17 at 23:03
  • This seems incredibly wrong. Why do you want to do this? – Makoto Dec 15 '17 at 23:08
  • Look, I agree 100% this is bad "design". At least, it's bad for code that needs to be read by humans. But if this is for generated code, I think I'll get a pass. (have anyone tried reading code that comes out of Antlr, for eg?) – One Two Three Dec 15 '17 at 23:29
  • Possible duplicate of [Getting hold of the outer class object from the inner class object](https://stackoverflow.com/questions/1816458/getting-hold-of-the-outer-class-object-from-the-inner-class-object) – Silvio Mayolo Dec 15 '17 at 23:38

3 Answers3

0

Seems like a not-too-good idea, but anyway, you'll need to explicitly export it yourself.

public static class Outer {
    public int field;
    public class Inner {
        public Outer outer() {
            return Outer.this;
        }
    }
}

//  caller methods
public static void foo(Outer.Inner inner) {
   System.out.println(inner.outer().field);
}
Leo Aso
  • 11,898
  • 3
  • 25
  • 46
0

This could work - bad coding practice anyway

 public static class Outer {
        public int field;
        public class Inner {
            public Outer outer=Outer.this
        }
 }


 //  caller methods
 public static void foo(Outer.Inner inner) {
       System.out.println(inner.Outer.field);
       System.out.println(inner.Outer.this.field);
 }
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
0

One option is mentioned in the other answers. If for some reason it doesn't fit your case, you can expose field from Inner, with getter (and setter if you need to):

public Outer class {
    private int field;
    public class Inner {
        public int getOuterField() {
            return field;
        }
    }
}

A different approach would to be pass the corresponding Outer instance wherever you need to use Inner, so foo will take two arguments in this case.

And yeah, I agree with all the comments posted - all of the options indicate bad design.

khachik
  • 28,112
  • 9
  • 59
  • 94