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 why only one public class per source file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why only one public class per source file?" Watch "why only one public class per source file?" New topic

why only one public class per source file?

Olly Ontario

Joined: Mar 17, 2008
Posts: 18
Hello there,
I have just picked up learning Java. . .
and I just wanted to know what was the thinking behind the developers to make only one public class per file and also give the file name as per the public class.

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Hi, welcome to the ranch. You may not be aware of the ranch's Naming Policy, so please read it carefully and change your name accordingly. (you need to set both first and last names). Thank you.

[My Blog]
All roads lead to JavaRanch
Olly Ontario

Joined: Mar 17, 2008
Posts: 18
Thanks Chris!. . .Have done accordingly!
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

*I* think the point is that each compilation unit has a single public interface represented by that public class. Now, this allows the source for a given class to be found by a compiler or a debugger e.g. when you compile a single class the compiler will search for source files for external references that might not have been compiled, or have later modified many times than the coresponding class files, and compiles them too.
Thus when a debugger halts at a statement it can it can provide the actual point of discrepancy.
Joe Ess

Joined: Oct 29, 2001
Posts: 8996

When packages are stored in a file system (�7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:

* The type is referred to by code in other compilation units of the package in which the type is declared.
* The type is declared public (and therefore is potentially accessible from code in other packages).

This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type wet.sprocket.Toad would be found in a file in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory.

The Java Language Specification

[How To Ask Questions On JavaRanch]
Olly Ontario

Joined: Mar 17, 2008
Posts: 18
Thank you!, Anubhav & Joe for you replies.
Its a bit difficult for me to grasp your answers as they have lots of technical jargon.
1. The Java compiler in itself is a very very complex piece of software, so...putting a restricting on the Java developer to name his file just for the sake of get the compiler to work a bit easier isn't fitting into my understanding.
2. Even so what about the non public classes that a programmer can write into his source files, I mean in addition to the single public class there may be lots on other classes too!. . .
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
It was an arbitrary decision made quite a while ago.

There are obviously benefits to having it either way.

Those who designed it fell to one side of that fence.

Bill Shirley - bshirley -
if (Posts < 30) JavaRanchFAQ);
marc weber

Joined: Aug 31, 2004
Posts: 11343

I don't think it was arbitrary. An early Oak Language Specification (version 0.2) suggests this was a requirement for optimization.
This restriction is not yet enforced by the compiler, although it's necessary for efficient package importation.

Ref: Once Upon an Oak...

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: why only one public class per source file?