| Author |
private class
|
Ritesh Badwaik
Greenhorn
Joined: Feb 23, 2009
Posts: 16
|
|
hi all,
Why a top level class in java be a private or protected ?
Thanks
Ritesh
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
|
A top level class must be marked public ... Otherwise the compiler will choke.
|
JDBCSupport - An easy to use, light-weight JDBC framework -
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3090
|
|
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.
|
Steve
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
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
Joined: Oct 27, 2005
Posts: 19232
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: private class
|
|
|