This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Top-Level Non Inner Class!" Watch "Top-Level Non Inner Class!" New topic
Author

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?
Thanks.
[ 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/Toad.java file. But if the wet/sprocket/Toad.java file could contain another public class/interface, eg Frog, the compiler has no knowledge how to find a source file for Frog (Toad.java?!).
This is an "optional restriction on compilation units in file-based implementations."
[ July 01, 2004: Message edited by: Irina Goble ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Top-Level Non Inner Class!
 
Similar Threads
java
What is difference between Top Level Class and Super Class?
Public Classes
2 classes in one file. Is that possible?
What will be the name of .java file?