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?
}
}
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.
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.
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.
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.
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"
}
}
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.
}
}
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With