Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP 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:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

private class

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
Why a top level class in java be a private or protected ?

Thanks
Ritesh
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A top level class must be marked public ... Otherwise the compiler will choke.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the Java Language Specs:

8.1.1 Class Modifiers
Not all modifiers are applicable to all kinds of class declarations. 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. The access modifier static pertains only to member classes (§8.5, §9.5).



So to the question: "Why a top level class in java be a private or protected ?" The answer is top level classes can not be private or protected, only member classes can be.

Sebastian:
"A top level class must be marked public ... Otherwise the compiler will choke."

A top level class can also be marked with no access modifier, which makes it accessible only to other classes inside the same package.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sebastian:
"A top level class must be marked public ... Otherwise the compiler will choke."

A top level class can also be marked with no access modifier, which makes it accessible only to other classes inside the same package.



true ;-)
 
Sheriff
Posts: 22080
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A private anything can only be accessed from code within the class itself. That means that a private top-level class would be a stand-alone class that couldn't even be found by the JVM. Not much use for that, now is there?

A protected anything can only be accessed from code within the class itself, the same package or any (direct or indirect) subclasses. Let's ignore the first two (default access, which is allowed, takes care of that). That leaves subclasses in other packages. A protected class could only be found by subclasses in other packages. But these subclasses cannot find the class in the first place, so they can't even be subclasses. Therefore protected also makes no sense.

That leaves public and default access, both of which do make sense, so these are allowed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic