Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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?

    }
}
like image 895
rage Avatar asked Sep 03 '12 17:09

rage


People also ask

Can inner class access outer class method?

Method Local inner classes can't use a local variable of the outer method until that local variable is not declared as final. For example, the following code generates a compiler error.

How do you access the outer class variable in an inner class?

If you want your inner class to access outer class instance variables then in the constructor for the inner class, include an argument that is a reference to the outer class instance. The outer class invokes the inner class constructor passing this as that argument.

Can static inner class access outer class variables?

Unlike inner class, a static nested class cannot access the member variables of the outer class. It is because the static nested class doesn't require you to create an instance of the outer class.

Can inner class access outer class variables Python?

Python allows for nested class definitions, which are classes defined within the definition of another class. Attributes of the outer class can be accessed from within the inner class.


3 Answers

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"
    }
}
like image 88
Sandeep Panda Avatar answered Sep 28 '22 15:09

Sandeep Panda


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.   

    }  
}
like image 35
Gene Avatar answered Sep 28 '22 15:09

Gene


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;
    }
}
like image 43
Bharat Sinha Avatar answered Sep 28 '22 17:09

Bharat Sinha