Hi ,
The o/p is 12,12
Explanation:
Even though i is declared as private , since both the method main and class Inner belongs to the class Test ... main can access private member of class Inner.
if the following code is in another class then..i will not compile
TestInner test = new TestInner();
TestInner.inner.i++;
System.out.println(TestInner.inner.i + "," + test.inner.i);
you can try compiling following program and see the output :
public class TestInner {
public static void main(String[] args) {
TestInner test = new TestInner();
TestInner.inner.i++;
System.out.println(TestInner.inner.i + "," + test.inner.i);
}
static class Inner {
private int i = 11;
}
static Inner inner = new Inner();
}
class InnerAccessor {
public InnerAccessor() {
TestInner test = new TestInner();
TestInner.inner.i++;
System.out.println(TestInner.inner.i + "," + test.inner.i);
}
}
it will not compile ...