wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes private and protected class? 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 "private and protected class?" Watch "private and protected class?" New topic
Author

private and protected class?

Lalchand omprakash
Greenhorn

Joined: May 30, 2009
Posts: 12
may i know the reason why a class in Java can't be declared as private or protected?

Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3023
    
  35

Inner classes CAN be declared as private or protected - that's same as for the attributes of a class.
What is the use you are expecting by making a top level class private, or protected?


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Abhinav Yadav
Ranch Hand

Joined: Nov 08, 2009
Posts: 34
there are 4 categories of nested classes,defined by the context these nested types are declared in
1). static member classes
2). non-static member classes
3). local classes
4). anonymous classes
out of which only static member classes and non-static member classes can have all types of accessibility modifiers i.e public private protected and default/package accessibility.
And local classes as well as anonymous classes can not use any of the access modifiers,and the top level class under which these nested types are declared can have public or default accessibility, a class an also have abstract, and final as access modifiers.
If a top level class is declared as private the compiler will complain that the "modifier private is not allowed here". This means that a top level class can not be private. Same is the case with protected.
a short example here shows the overview of type declarations:

I hope this clarifies your doubt to some extent


No Dream Too Big
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: private and protected class?
 
Similar Threads
How come public and friendly(default) access is allowed for top level class but not
Roundup game
classes cannot be marked as protected?????
Top level class Vs inner classes access specifiers