permaculture playing cards*
The moose likes Ranch Office and the fly likes Quirk in Rule Round-up and comment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Ranch Office
Bookmark "Quirk in Rule Round-up and comment" Watch "Quirk in Rule Round-up and comment" New topic
Author

Quirk in Rule Round-up and comment

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I am running NT 4.00.1381 and Netscapr 4.6. Occasionally the question does not display (the cows and radio buttons display fine). Hitting reload once (or twice) gets the questioned to display.
Comment on question 232: TRUE or FALSE: an inner class has free access to ALL member data of its enclosing (outer) class.
Since it was not specified that the inner class was a non-static, non-local inner class I would think the answer would be false (the round-up answer is true). Should the wording of this question be changed to reflect that the inner class is non-static and non-local?
Thanks.
BTW, the site is really cool.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20582
    ∞

There are some known bugs with the applet. I have it pulled apart at home although it will probably be a week or more until I can fix it.
Question 232: TRUE or FALSE: an inner class has free access to ALL member data of its enclosing (outer) class.
Answer: TRUE. An inner class has a special relationship and can see even private member data of the outer class.
By non-local I take it you are talking about classes within a method?
I see your point about static inner classes. This question is definitely ambiguous.
Maybe the question should be changed to "a static inner class has free access to ALL member data of its enclosing (outer) class." and then the answer will be false.

permaculture Wood Burning Stoves 2.0 - 4-DVD set
John, Wetherbie
Greenhorn

Joined: Apr 05, 2000
Posts: 2
Originally posted by Paul Wheaton:

By non-local I take it you are talking about classes within a method?
I see your point about static inner classes. This question is definitely ambiguous.
Maybe the question should be changed to "a static inner class has free access to ALL member data of its enclosing (outer) class." and then the answer will be false.

I use the term "local" to indicate that the inner class is declared/used in a method of the enclosing class. In this case the inner class can only access variables/parameters that have been declared final in the enclosing class.
Your proposed change in wording would certainly work. You could also add "non-static" and then the answer would remain true.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Paul- "By non-local I take it you are talking about classes within a method?"
I would assume that non-local means classes outside a method. It is unnecessary to specify this though. Local classes (which includes anonymous classes) have access to all member data of the enclosing class, so for them the answer is TRUE, just as it is for all non-static member classes. These are the only classes properly called inner classes, but thanks to some unfortunate inconsistency from Sun, "static inner" classes are sometimes considered "inner" classes as well. They should be called top-level nested, or static nested, or static member (new term from JLS 2nd Ed). In any event they should not be considered inner, but since there is widespread confusion about this perhaps it would be best to clarify this point in the question:
TRUE or FALSE: an inner class (not including a top-level nested class) has free access to ALL member data of its enclosing (outer) class.
Answer: TRUE.
Or you could change it to only ask about TLNCs as you suggest, but I like this one better. There is a widespread myth (illustrated by Mr. Wetherbie above - sorry John) that a local class can't access member fields of the outer class unless they're final. It's true local classes can't access a local variable of the enclosing method (or code block) unless it's final - but that has nothing to do with member variables. Keeping the question along its original intent catches the people who think this, and hopefully they learn.
[This message has been edited by Jim Yingst (edited April 05, 2000).]


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

Joined: Dec 14, 1998
Posts: 20582
    ∞

Jim,
Oops! Slipped a negative in there.
I think one thing we need to remember about the questions in the roudup game is that they have to be short (due to real estate restraints) and they can't be giving the answer away. If you have a lot of conditions in your question, one can guess that the answer is true.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Good point. How about
TRUE or FALSE: a non-static nested class has free access to ALL member data of its enclosing (outer) class.
Or, a "non-static inner class" works also, though it's technically redundant. But given the widespread confusion Sun has caused on this point, redundancy is a good thing.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20582
    ∞

Done.
I also added the word "always" because test takers think "always" questions are false.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Oooh, devious...
John, Wetherbie
Greenhorn

Joined: Apr 05, 2000
Posts: 2
Hey, thanks for clearing this "myth" up for me! Of course, I didn't know it was a myth. The certification book I am using has this to say:
"Because local inner classes are defined local to a code block (like a local variable), they may only access the local variables or method parameters of the code block in which they are defined. An additional restriction is placed on local inner classes in that they may only access variables or parameters that are declared as final and have been assigned a value."
(p 146, Java 2 Certification, J. Jaworski)
It's possible that I am misinterpretting this...
As far as the term "static inner" class goes, it is misleading. Static member class may help (still seems misleading). How about top-level nested static class?
Jim - thanks for clearing up my confusion on inner classes inside a method (or scope block) or whatever you want to call them.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It doesn't sound like you're misinterpreting - from what you've quoted, Jaworski is just plain wrong here. Does anyone know of an errata list for Jaworski? If not, that's an much bigger mark against Jaworski than the original error, IMO. RHE, Brogden, and Mughal/Rasmussen all keep errata (RHE's seems unusally long, but at least they have one ).
Yeah, I don't really like "static inner" (can you tell?) but it seems to have caught on. Probably because most of the literature emphasized "inner" instead of "nested", people are more familiar with the former term. And the specs only mention as an afterthought that a nested class that's static isn't inner, but rather top-level. Also people like to have short terms for things - "static inner" is much shorter than "top-level nested", and comes closer to describing what one actually looks like. "Static nested" would have worked as well, but it wasn't the term the spec writers went with. Ah well... However:
It looks like the JLS 2nd edition will in fact use "static member class" in place of "top-level nested class". "Top-level" goes back to meaning really top-level, i.e. not nested. However the definition of "inner" is not changing - a non-static member class is an inner class, but a static member class is not. So that particular religious war will still go on, but those of us on the side of truth and light will have shiny new ammo.
Hmmm... a non-static member variable is also known as an instance variable, and a static member variable is also known as a class variable. By extension, the respective nested class equivalents could be known as an "instance class" and a "class class" respectively. Surely that would clear up the confusion.
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
The book has a URL that is supposed to have errata. The page is there but there is no errata. I also went out to the publisher's (New Riders) site and the page that might have the errata is down for reconstruction.
More interesting is that I can't find any mention of Jaworski's book at the New Riders site.


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20582
    ∞

I say "fire up the compiler and make sure!"
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
Paul, you're right on this point. It always pays to test things out with code. So, that's what I did last night to check this issue out.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20582
    ∞

And?
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
Jim was right on the money.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20582
    ∞

Jim, I hereby DOUBLE YOUR SALARY!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Golly, that probably puts me in a higher tax bracket now.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20582
    ∞

I may be a bit shaky in my math, but isn't zero times two equal to zero?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Party pooper.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quirk in Rule Round-up and comment