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 Rules Round Up Game ( Question No.235) 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 "Rules Round Up Game ( Question No.235)" Watch "Rules Round Up Game ( Question No.235)" New topic

Rules Round Up Game ( Question No.235)

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I have a slight problem with the following question on the Rules Round Up Game
TRUE or FALSE : a static inner class (considered a top - level nested class) can NOT access non-static variables of the outer class.
The correct answer, as given by the tester is TRUE. But I am of the opinion that it should be FALSE because as a matter of fact, a static inner class may access non static variables of the outer class if an instance of the outer class is used to retrieve the variable as in
class Outer {
private theValue = 20;//non static variable of Outer

public static void main(String[] args){
Inner inner = new Inner();
static class Inner {
//Lets attempt to access non static variable of
//Outer class from this static inner class
System.out.println(new outer().theValue);
}//End of Inner Class
}//End of Outer Class
I appreciate the concept that I think this question is trying to access, but I thought it would be more accurate if it included "without using an instance of the outer class".
Tell me what you think about this and for the sake of the exam which I am preparing for, please advise on what to answer if I meet a question with such wording.
Thanks comrades.
paul wheaton

Joined: Dec 14, 1998
Posts: 20965

Everything you say is correct.
Maybe the question should be rewritten to be an example:
public class Outer
int x ;
static class Inner
void foo()
x = 5 ;
Then the answers could be "compiles". "Does not compile". What do you think?

permaculture Wood Burning Stoves 2.0 - 4-DVD set
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Rules Round Up Game ( Question No.235)
jQuery in Action, 3rd edition