wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What's the deal with private classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What Watch "What New topic
Author

What's the deal with private classes

Nithya Natarajan
Ranch Hand

Joined: Feb 25, 2002
Posts: 30
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.


Thanks<br />Nithya<br />Sun Certified Java 2 Programmer
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Nithya Natarajan
Ranch Hand

Joined: Feb 25, 2002
Posts: 30
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

Joined: Aug 26, 2001
Posts: 7610
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)
 
Consider Paul's rocket mass heater.
 
subject: What's the deal with private classes
 
Similar Threads
Private member access
abstract class
Class as private?
Sorting an array
Errors due to private & protected top level class