Question 235, as stated, is not correct. It reads: (#235) TRUE or FALSE: a static inner class (considered a top-level nested class) can NOT access non-static variables of the outer class. The desired answer is TRUE, but in practice, is false. The following code shows how, with Outer being the outer class, three being the non-static variable, and Inner being the static inner class: public class Outer { protected static Outer self = null; protected int three = 3; public Outer() { self = this; Inner.doIt(); } protected static class Inner { protected static void doIt() { if (Outer.self != null) { System.out.println(Outer.self.three); } } } }
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18641
posted
0
You are correct Steve. I think the intended meaning of the question would be clearer with this wording: (#235) TRUE or FALSE: a static inner class (considered a top-level nested class) can NOT access non-static variables of the outer class without using an explicit reference to an instance of the outer class. (TRUE) Of course, that makes it longer ... Maybe it would be easier to ask instead the converse: TRUE or FALSE: a non-static inner class can access non-static variables of the outer class without using an explicit reference to an instance of the outer class. (TRUE)
This was once discussed before: http://www.javaranch.com/ubb/Forum24/HTML/001339.html Although nothing came of it. I suggested that the question be rewritten as <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?
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18641
posted
0
Works for me.
steve hitch
Greenhorn
Joined: Jun 01, 2000
Posts: 2
posted
0
Agree completely!
Brusk Baran
Ranch Hand
Joined: Nov 15, 2001
Posts: 132
posted
0
Using j2sdk1-4-0 and DOES NOT COMPILE.. AND MUST NOT COMPILE.. In 2000, compilers were behaving strange then!!! javascript: x() Eek!
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 8997
posted
0
Hard to format this code in a text file.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt