Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What's the deal with private classes

 
Nithya Natarajan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I declared a class as private and tried to compile it. It gave me an error:
"modifier private not allowed here"
Class definition:
private class MyClass {}
Can anyone please tell me what's wrong in declaring a class as private? Because I thought a class can be declared as private.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
top-level classes cannot be declared private. What would you do with such one?
From JLS 8.1.1 Class Modifiers

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
 
Nithya Natarajan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when a Question is asked such as:
which keywords can be used as modifiers in the declaration of method in a class ?
and "private" is one of the options, Should I select the "private" option or not?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private CAN be an access modifier for methods but not for top-level classes, only for inner classes (that is classes enclosed within another class)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic