File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes top-level class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "top-level class" Watch "top-level class" New topic
Author

top-level class

Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
who can explain what the top-level class is?


help you means help me
Mike Beaty
Greenhorn

Joined: Dec 19, 2001
Posts: 20
A top level class is any class that is not enclosed inside another class. Top-level classes can be public or default.
for example:


Mike Beaty<br />Sun Certified Programmer for the Java 2 Platform
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
In a file, there may be more than 1 top-level class,isn't it?
and cann't be private or protected?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Seany,
yes, in a file you can have many top-level classes. Of course, you can only have at most ONE public class, and the file name has to be the same as this public class. So you could have a file full of classes with default access, or many default access classes and ONE public class.
Rob


Rob
SCJP 1.4
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
yes, i see.
thank you all!
Uday Kumar
Greenhorn

Joined: Jan 22, 2002
Posts: 24
"In a file, there may be more than 1 top-level class,isn't it?
and cann't be private or protected?"
What could be the reason for a top-level class cannot be protected?
[ January 31, 2002: Message edited by: Uday Kumar ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Uday Kumar:
"In a file, there may be more than 1 top-level class,isn't it?
and cann't be private or protected?"
What could be the reason for a top-level class cannot be protected?
[ January 31, 2002: Message edited by: Uday Kumar ]


Hi Uday
There are couple of arguments I want to put forth, but I am not sure about it, somebody needs to second me over here....(Val please Help!!!)
1) the JLS wants it to be and we have to adhere to it.
2) The Class Loader can only load public or default-access upper level classes. Somebody might argue that inner class are private and the class loader can still load it, but in order to load an inner class, the class loader has to first load the inner-class's upper class. And the class loader treats an inner class as a member of the upper class.
This is my guess........
Bartenders please help!!!
Amish
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Uday Kumar:
"In a file, there may be more than 1 top-level class,isn't it?
and cann't be private or protected?"
What could be the reason for a top-level class cannot be protected?
[ January 31, 2002: Message edited by: Uday Kumar ]


Well, think about it logically.
If you have a top level class marked private, who can use it?
private class foo()
{
int anInt = 12;
}
Private means that only the class can access it. That means that only foo can access itslef. No other class could ever do anything with foo. That doesn't seem very useful does it?
Similarly, with protected, you're saying that classes within the same package can access it. But the default access already covers package access, so in this case supporting protected would be redundant. More serious however, is that if you allowed protected top-level classes, you would have a situation where outside the package, only subclasses of the class could instantiate the class; this would include constructors, so that would effectively prohibit you from instantiating a subclass that was not in the same package. Again, this is redundant behaviour, as the default access already provides for this functionality.
Rob
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thank you Rob, and I was thinking way hard :roll: ....phew
This makes it much clear!!!
Amish
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: top-level class