Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rules Round Up Game ( Question No.235)

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21378
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic