Forums Register Login

Top level class access modifier

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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)...
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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) ]
+Pie Number of slices to send: Send
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.
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 9843 times.
Similar Threads
Top level Class can be private,protected?
question regarding modifiers applicable to top level class
Top Level Classes
Question on Access Modifiers
Top level class??
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:54:36.