I thought inner classes could access the outer class variables/methods?

前端 未结 5 1157
后悔当初
后悔当初 2020-12-09 10:18

I did read a number of topics discussing inner classes, and i was under the impression that an inner class has access to the variables and methods of the enclosing class. In

5条回答
  •  鱼传尺愫
    2020-12-09 10:44

    System.out.println(inner.a);
    

    You are attempting to read a as if it is a property of you inner class, but it's not. You should define a method that will read the desired property of the outer class. So in your inner class, you should have:

     public String getA(){
         return a;
     }
    

    System.out.println(inner.getA());
    

    This should work.

提交回复
热议问题