• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Errors due to private & protected top level class

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't give a top-level class anything but public or nothing (ie package) access.
-Barry
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
He does not suffer fools gladly. But this tiny ad does:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic