• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What's the deal with private classes

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
reply
    Bookmark Topic Watch Topic
  • New Topic