Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

private and protected class?  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may i know the reason why a class in Java can't be declared as private or protected?

 
Marshal
Posts: 5967
917
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!