[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
posted
0
You can't give a top-level class anything but public or nothing (ie package) access. -Barry
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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: