• Post Reply Bookmark Topic Watch Topic
  • New Topic

Modifiers for inner classes  RSS feed

 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
This is a question form dan's exam.

Which of the following modifiers can be applied to a class that is not a nested class?
a. public
b. protected
c. private
d. abstract
e. static
f. final

I got some doubts here. Could anyone point the modifiers allowed for:
- ordinary inner class
- method local inner class
- anonymous class (none, I guess)

I really thought the answers for this would be: a, b, c, d, f.
Thanks.
[ March 14, 2007: Message edited by: Leandro Melo ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From JLS 8.1.1:
The access modifier public pertains only to top level classes (�7.6) and to member classes (�8.5, �9.5), and is discussed in �6.6, �8.5 and �9.5. The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration (�8.5) and are discussed in �8.5.1. The access modifier static pertains only to member classes (�8.5, �9.5). A compile-time error occurs if the same modifier appears more than once in a class declaration.

There is no place to put modifiers in an anonymous class. Other than that, abstract and final are not restricted in terms of your categories, but there are certainly cases where they don't work.
 
Ren� Star
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Leandro Melo:
Hi.
This is a question form dan's exam.

Which of the following modifiers can be applied to a class that is not a nested class?
a. public
b. protected
c. private
d. abstract
e. static
f. final


Here's the list for the accessibility classes (public, private, protected):

1. Top-level class
public or none(=default/package accessibility)

2. Member class (nested class that is not inside a method or other block)
public, private, protected or none

3. Local class (class inside a block)
4. Anonymous class
For both types no accesibility modifiers are allowed.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A member class of an interface can not have any explicit access modifier, except public. Explicitly specifying it is redundant.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!