aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Errors due to private & protected top level class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Errors due to private & protected top level class " Watch "Errors due to private & protected top level class " New topic
Author

Errors due to private & protected top level class

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
[Code]class topclass{}[/Code}
When I declare topclass as private it gives compiler error saying "The type type topclass can't be private. Package members are always accessible within the current package".
When I declare it as protected it gives following compiler error
Class or interface declaration expected.
protected class topclass{}
^
What does compiler actually mean?Why doesn't it give compiler error saying that topclass can't be protected?
Thanks
Veena


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You can't give a top-level class anything but public or nothing (ie package) access.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Kumar Kausikasa
Ranch Hand

Joined: Jun 04, 2002
Posts: 45
Hi,
This is how it work:
1. You write a java Program
2. Compile it using javac yourprogram.java
When you compile it , java compiler is generating a byte code (just 0's and 1's) from the program you have written in java.javac generates a class file, which is used for running
3. Then you actually run it by java yourprogram
Compiler error means, this is not able to gnerate a .class file because the program you have written is not legal or syntax you have used is not allowed in the language
In java top class can only be public, so it is giving you compiler error.
For details see RHE book
HTH
Praveen
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
I know top level class can't be protected & private.I wanted to know why complier gives different explanations for private top level class & protected top level class??
Thanks
Veena
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Oh, that's advanced
My guess is that the guy who programmed the compiler when home, and someone else finished it off.
That's semi-serious: when you see some of the inconsistancies abound in the different member function names and field names it'll make you shudder. See Roedy Green's Gotchas
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
By the way, knowing that top-level classes cannot be declared "private" or "protected" is very important for the SCJP 1.2 certification exam.
When I took it last week, there were two different questions with long code samples, asking what the code did. One of them had a top-level class declared "private", the other had a top-level class declared "protected". In both cases, the correct answer was "It causes a compiler error on line X".


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
When you guys are using term "Top-level class", are you refering to top class in the class hierarchy or top class as opposed to inner class.
I thought there are only two legal access
specifier for class; public or package.
The private and protected are meant for member
methods and data members. Am I off the base?
Thanks
Barkat
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
By "top-level class" I mean a class declared at the outer level -- not nested, not inner.
A nested or inner class can be declared private or protected.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
By "top-level class" I mean a class declared at the outer level -- not nested, not inner.
Nested classes, that is, classes declared static enclosed in other classes are also top-level. All classes that can be instantiated without needing an instance of the enclosing type are top-level classes. The only difference is that nested classes may be declared protected or private whereas classes at level 0 (not enclosed in any other type) may not.
For instance:


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Errors due to private & protected top level class