• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class problem

 
Abhishek Sharma
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question states:::

What will be the o/p


public class Test {
public static void main(String[] args) {
Test test = new Test();
test.inner.i++;
System.out.println(Test.inner.i + "," + test.inner.i);
}
static class Inner {
private int i = 11;
}
static Inner inner = new Inner();
}
the correct answer is 12, 12

In this case are we able to access the private variable of inner class ???
Plz expalin why.....as private variables r not accessible outside the class

Thanks and Regards
Abhishek
 
Greg Charles
Sheriff
Posts: 2993
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you're not really outside the class. You access it from the main method in Test, and Inner is enclosed by Test. Still, the access rules for private variables in inner classes is far from clear cut. I seem to remember, in the early days of Java, they needed a number of compiler tweaks to make the access rules work to specification.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use Code Tags to keep your formatting intact. This will make it easier for others to read.
[ February 21, 2006: Message edited by: marc weber ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic