• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Rules Round Up Game ( Question No.235)

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Trailboss
Posts: 23782
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic