aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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
Author

Rules Round Up Game ( Question No.235)

Anonymous
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 {
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
Trailboss

Joined: Dec 14, 1998
Posts: 20271
    ∞

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


permaculture Wood Burning Stoves 2.0 - 4-DVD set
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rules Round Up Game ( Question No.235)
 
Similar Threads
Bad Java Rule Round-up question (#235)
Quirk in Rule Round-up and comment
Inner class question in Rules Round-up
Inner class
Round - Up game