aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why should there be only one public class per source file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why should there be only one public class per source file" Watch "Why should there be only one public class per source file" New topic
Author

Why should there be only one public class per source file

Ganesh Kumar
Greenhorn

Joined: Dec 18, 2006
Posts: 11
Why should there be only one public class per source file?
Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
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 ...


Gunjan Kumar
SCJP 1.5
Cor Takken
Ranch Hand

Joined: May 21, 2007
Posts: 58
Official answer: Because SUN said so.

Learn this and get that part of the certification correct (if that question were to appear).

My guess is that there should only be one 'entry point' into a class file, easier for the compiler. Say that more than one public class is present in a file and the command java classname is issued; which class file should be used? The filename doesn't represent the callable class anymore, so how to relate the class to run to the file in which the class is specified? If you were to solve this, you have to change the 'java' command (and the compiler) and make some effort to specify which of the 'main' methods to activate. By setting the specification as SUN has done, you keep things much simpler.


SCJP 1.5
Dolphins are grey, but they dream in colour.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

I recently learned here that there is a historical reason why this is so. Long ago, when Java wasn't yet called Java (it was called Oak at that time) this was necessary for the compiler to be able to import packages efficiently.

From here:
Why is each public class in a separate file? (Section 1)

This is a question that I have frequently been asked during my courses. Up to now I have not had a good answer to this question. In section 1, we read: "Although each Oak compilation unit can contain multiple classes or interfaces, at most one class or interface per compilation unit can be public".

In the sidebar it explains why: "This restriction is not yet enforced by the compiler, although it's necessary for efficient package importation"

It's pretty obvious - like most things are once you know the design reasons - the compiler would have to make an additional pass through all the compilation units (.java files) to figure out what classes were where, and that would make the compilation even slower.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
This is to tell the JVM which class contains the main() method and where to begin the execution from.


SCJP 5.0, SCWCD in progress
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
Originally posted by Mohit Jain:
This is to tell the JVM which class contains the main() method and where to begin the execution from.


No, this requirement is for all classes, not just for applications. It is kind of an arbitrary requirement, and there used to be compilers that didn't enforce it (I think the Metrowerks IDE didn't care about it).


Ping & DNS - my free Android networking tools app
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Why should there be only one public class per source file