• 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

Top level class access modifier

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On javaprepare.com I found the following question :
--
A top level class may have only the following access modifier. Select one correct answer.
A package
B friendly
C private
D protected
E public
--
They say the correct answer being E but what about the following class which compiles and runs.
-- File Simple.java ----
class Simple
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
In this case, the top level class has a package access !!!
I'm a bit puzzled and any help would be welcome
Regards
A. B.
 
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
You are right Anselme.
As per JLS 7.6 Top Level Type Declarations a top-level class is allowed to have a package-wide access (i.e. no modifier specified)...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But "package" is not a legal modifier - it's a keyword meaning something else. I think that's part of what the question is intended to test. So answer E is correct. However, the question is not as clear as it could have been.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI
BASICALLY PACKAGE IS NOT A MODIFIER BUT DEFAULT MODIFIER IS USED FOR PACKAGE LEVEL.THATS Y V CAN SAY IT PACKAGE LEVEL MODIFIER.
BUT FREINDLY IS NOT A MODIFIER IN JAVA BUT DEFAULT MODIFIER IS WORKED AS A FRIENDLY MODIFIER BUT SUN MICRO SYSTEM DOESNT RECOMMEND THE USE OF KEYWORD OF FRIENDLY AS A MODIFEIR.
[ August 09, 2002: Message edited by: Khurram Shahood(SCJP2) ]
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question has given u several choices out of which u have select the correct answers. The answer E is absolutely right and if they had given the default modifier(no keyword for this one) as another choice then it would also have been another answer.
 
reply
    Bookmark Topic Watch Topic
  • New Topic