Two Laptop Bag*
The moose likes Ranch Office and the fly likes Bad Java Rule Round-up question (#235) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » This Site » Ranch Office
Bookmark "Bad Java Rule Round-up question (#235)" Watch "Bad Java Rule Round-up question (#235)" New topic
Author

Bad Java Rule Round-up question (#235)

steve hitch
Greenhorn

Joined: Jun 01, 2000
Posts: 2
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: 18671
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)

"I'm not back." - Bill Harding, Twister
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20489
    ∞

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?


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Works for me.
steve hitch
Greenhorn

Joined: Jun 01, 2000
Posts: 2
Agree completely!
Brusk Baran
Ranch Hand

Joined: Nov 15, 2001
Posts: 132
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: 9044
    
  10
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bad Java Rule Round-up question (#235)
 
Similar Threads
Inner Class - Initialization Sequence
inner classes see everything?
Rules Round Up Game ( Question No.235)
Inner class
Java Rule Round-up Question 136