File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Top-Level Non Inner Class! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Top-Level Non Inner Class!" Watch "Top-Level Non Inner Class!" New topic

Top-Level Non Inner Class!

Tulsi Rai
Ranch Hand

Joined: Jun 26, 2002
Posts: 55
Hi All,
Java says "We can have as many top-level classes in one source file as you we like, but ONLY one can be declared public and the source file name must match the public class name."

WHY the all class can't be declared PUBLIC? could you please explain?
[ July 01, 2004: Message edited by: Tulsi Rai ]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
public classes are located by searching the directories in your CLASSPATH for class files with a filename starting with your class name. If a source file had more than one public class, which name would javac give the class file?

Mike Gershman
SCJP 1.4, SCWCD in process
Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
The answer can be found here.
So the compiler will look for a public wet.sprocket.Toad class/interface in the wet/sprocket/ file. But if the wet/sprocket/ file could contain another public class/interface, eg Frog, the compiler has no knowledge how to find a source file for Frog (!).
This is an "optional restriction on compilation units in file-based implementations."
[ July 01, 2004: Message edited by: Irina Goble ]
I agree. Here's the link:
subject: Top-Level Non Inner Class!
jQuery in Action, 3rd edition