Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private class

 
Ritesh Badwaik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
Why a top level class in java be a private or protected ?

Thanks
Ritesh
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A top level class must be marked public ... Otherwise the compiler will choke.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
  • 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 ;-)
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic