• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Errors due to private & protected top level class

 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't give a top-level class anything but public or nothing (ie package) access.
-Barry
 
Kumar Kausikasa
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic