Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Top-Level Non Inner Class!

 
Tulsi Rai
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Irina Goble
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic