This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes java concepts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java concepts" Watch "java concepts" New topic
Author

java concepts

Abhishek kumar Singh
Greenhorn

Joined: Jan 17, 2009
Posts: 4
why there can be only one public class in a source code file?

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11815
    
  18

This has been asked many times in this forum. You may want to try searching for more details, but the short and simple answer is because it makes writing the compiler easier.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
There can be only 1 public upper level class in a .java file because javac references the file by the name for compiling when delared public. The .java file containing the public class must have the same name as that of the class. Moreover, a file can have only 1 name, so only 1 public class

But there can be any number of inner public classes.


Experience and talent are independent of age
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java concepts
 
It's not a secret anymore!