This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Top level class access modifier

 
Anselme Bender
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • 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)...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.
 
Khurram Shahood(SCJP2)
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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) ]
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic