Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaRanch Rules Roundup #17

 
paul wheaton
Trailboss
Pie
Posts: 21880
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a top-level class be marked as private?
The provided answer is "no".
A static inner class is considered to be a top level class and it can be marked private. Therefore, this answer is wrong.
Somebody should notify management!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than changing the answer though, I'd rephrase the question slightly. Replace "top-level class" with "regular top-level class", "top-level (non-nested) class", or something like that.
Annoyingly, Sun's terminology has left us without a concise unambiguous term for good old-fashioned top-level classes.
 
paul wheaton
Trailboss
Pie
Posts: 21880
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer should be changed to "yes".
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've heard "package level" applied to the classes whose fully qualified names consist of only the package name and ONE class name...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I forgot that one. I don't think Sun officially defined it though, so it's not as well-known as I'd like. As for whether to re-write the question or the answer, I suppose either one works, depending on what you want to test people on. I think people should know that (a) package-level classes cannot be private, and (b) nested top-level classes can be private. Test on whichever fact you want, or both.
Curiously, package-level classes can be public or default (package) access, but not protected. This seems strange to me since protected is in between the other two in terms of permissiveness. Probably not a good test question though...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... the just-released draft of the JLS 2nd edition now seems to define top-level classes to exclude static nested classes (which will now also be known as static member classes, but not static inner classes). I think that's a good move to clear up some of the muddy usage that we've seen in the past - we'll see though if it stays that way in the final version. Of course this has no effect yet on the actual exam, which apparently does use the term "static inner class" even though the existing specs say that a static nested class is not an inner class. Sigh. So there will still be confusion on this issue for some time.
So, back to the issue at hand:
Q: Can a top-level class be marked as private?
A: The correct answer is yes right now, but it could well be no in the future.
Q: Can a top-level (regular or package-level) class be marked as private?
A: No.
 
Rich Wardwell
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest, one of the reasons I've always enjoyed the rules roundup was because it was fairly clear and unambiguous - trick questions were cunning but fair. A challenging question is certainly acceptable, but changing the answer on this one without clarifying the question all considering would be evil! My suggestion is to modify the question to make it more clear or remove the question altogether.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic