17

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 the following i have an outer class and an inner class, in the test class i create an instance of the outer class and then from that i create an instance of the inner class. However i am not able to access the String variable a through the inner class reference. Help?

public class OuterClass {

    String a = "A";
    String b = "B";
    String c = "C";

    class InnerClass {
        int x;

    }

    public static class StaticInnerClass {
        int x;
    }

    public String stringConCat() {
        return a + b + c;

    }
}

public class TestStatic {

    public static void main(String args[]) {

        OuterClass.StaticInnerClass staticClass = new OuterClass.StaticInnerClass();
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();

        System.out.println(inner.a);// error here, why can't i access the string
                                    // variable a here?

    }
}
Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48
rage
  • 1,777
  • 5
  • 25
  • 36

5 Answers5

23

The inner class can access the outer class methods and properties through its own methods. Look at the following code:

class OuterClass {

    String a = "A";
    String b = "B";
    String c = "C";

    class InnerClass{
        int x;
        public String getA(){
            return a; // access the variable a from outer class
        }
    }

    public static class StaticInnerClass{
        int x;
    }

    public String stringConCat(){
        return a + b + c;    
    }
}


public class Test{

    public static void main(String args[]) {

        OuterClass.StaticInnerClass staticClass = new OuterClass.StaticInnerClass();
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();

        System.out.println(inner.getA()); // This will print "A"
    }
}
Andreas Vinter-Hviid
  • 1,482
  • 1
  • 11
  • 27
Sandeep Panda
  • 723
  • 6
  • 9
  • 6
    Ah! I think i understand now. An inner class has access to the fields in the outer class INSIDE of its class declaration. But you cannot create an instance of an inner class and expect that the inner class reference has access to the outer classes fields. Is this correct? – rage Sep 03 '12 at 17:34
  • 1
    Yes..you got it correct! the inner class can access the properties/methods of outer class inside its methods. Note that the inner class can even access private properties and methods of outer class. – Sandeep Panda Sep 03 '12 at 17:44
  • Thanks, @SandeepPanda I have declared my class as private, I changed it to the public and followed your way to access inner class and it worked. – amit pandya Mar 28 '19 at 07:28
6

It seems you're confusing scope and access. References can access only the attributes and methods of the object to which they refer. So your inner.a can't work.

On the other hand, outer class variables are within the scope of methods in a respective inner class. So you can do what you want by defining a read accessor.

public class OuterClass {  

    String a = "A";  
    String b = "B";  
    String c = "C";  

    class InnerClass{  
        int x;  
        String getA() {
             return a;
        }
    }  
}  


public class TestStatic {  

    public static void main(String args[]) {  

        OuterClass.StaticInnerClass staticClass = new OuterClass.StaticInnerClass();  
        OuterClass outer = new OuterClass();  
        OuterClass.InnerClass inner = outer.new InnerClass();  

        System.out.println(inner.getA());  //error should be gone now.   

    }  
}
Gene
  • 46,253
  • 4
  • 58
  • 96
1

inner is an instance of OuterClass.InnerClass which doesn't defines a so it wont be able to access it anyways.

To understand it in the simplest manner... look at the block in which a is created and the block in which OuterClass.InnerClass is defined.

public class OuterClass { // a is defined in this block

    String a = "A";

    class InnerClass{ // InnerClass starts here and an instance will access what is defined now
        int x;
    }
}
Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
  • So, inner will only have access to the variables/methods defined within its own class declaration? – rage Sep 03 '12 at 17:28
  • So what is meant when it is said that an inner class has access to all the variables/methods in the outer class? Sorry, i'm still a bit confused. Edit: Nevermind, i understand now! – rage Sep 03 '12 at 17:31
0

An Non-static inner class has an implicit reference to the OuterClass.....

Try it out like this.....

class OuterClass {

    String a = "A";
    String b = "B";
    String c = "C";

    class InnerClass {

            String x = a;    // Directly uses the instance variable a from Outer class


    }

    public static class StaticInnerClass {
        int x;
    }

    public String stringConCat() {
        return a + b + c;

    }
}

public class TestStatic {

    public static void main(String args[]) {

        OuterClass.StaticInnerClass staticClass = new OuterClass.StaticInnerClass();
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();

        System.out.println(inner.x);

    }
}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0
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.

Joseph Elcid
  • 887
  • 1
  • 6
  • 21