aspose file tools*
The moose likes Beginning Java and the fly likes Top level classes 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 » Java » Beginning Java
Bookmark "Top level classes" Watch "Top level classes" New topic
Author

Top level classes

Kathy Rogers
Ranch Hand

Joined: Aug 04, 2000
Posts: 103
I'm a bit confused around some of the rules about top-level classes and what they actually are.
Is a top-level class just any class that is not an inner class?
Each public java class must be saved in a file with the same name as the java class - so you can only have one public top-level class per file. But you can have lots of top-level classes in one file as long as only one of them is public. It's bad practice but you can do it. Why is this? Especially as these top-level classes resolve themselves in separate .class files.
And my final query is, why can't top-level classes be protected? They can use the default access which is more restrictive so why not protected? Is it simply so that you can be lazy and create a top-level class without specifying an access modifier or is there a better reason?
Sorry, these are really niggly things but they've been puzzling me for ages and my SCJP date is rapidly approaching!
Thanks in advance,
Kathy
Sajan Joseph
Ranch Hand

Joined: Jan 05, 2001
Posts: 40
By defenition 'protected' means that a different package sub class of a class can access the memeber variable or methods that are marked as protected.
If you apply the same restriction of protected to a class, this will mean that a class can be used in a different package only if it is extended.
But when you write a class one motive is to allow the user of the class to use it as is via aggregation (and not only through inheritance). I feel this must be the reason why the architects decided not to give 'protected' accessibility option to classes.
On the other hand, default access means that the class is accessible only to other classes inside the same package. This makes sense from the package developers point of you. He does not want these classes to be accessible to any user. The class need be accessible only to other friendly classes in the package he/ she is developing.
Regards,
SJ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Top level classes