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 java concepts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java concepts" Watch "java concepts" New topic

java concepts

Abhishek kumar Singh

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

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

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
I agree. Here's the link:
subject: java concepts
It's not a secret anymore!