File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class problem" Watch "Inner class problem" New topic

Inner class problem

Abhishek Sharma
Ranch Hand

Joined: Jan 09, 2006
Posts: 68
The question states:::

What will be the o/p

public class Test {
public static void main(String[] args) {
Test test = new Test();
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 private variables r not accessible outside the class

Thanks and Regards
Greg Charles

Joined: Oct 01, 2001
Posts: 2968

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

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: Inner class problem
It's not a secret anymore!