This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes only one PUBLIC CLASS per source file..... But  Why..????? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "only one PUBLIC CLASS per source file..... But  Why..?????" Watch "only one PUBLIC CLASS per source file..... But  Why..?????" New topic
Author

only one PUBLIC CLASS per source file..... But Why..?????

waseem nadaf
Greenhorn

Joined: Mar 28, 2007
Posts: 10
Lets say i have ten java classes and i want all of them to be public. It implies(according to java) that i need to declare each of them in a different source file. But that is so irritating. Why is it so strictly compulsory..?? Will it be a problem if i have al my public classes in the same source file..?? According to java syntax, YES it is a problem... but what is the logical problem....??? ( Is it that just because the java guys wanted the file name to be the same as that of the public class they decided to have just one public class per source file.. I dont think thats the reason...)

Sorry for the lengthy question...

Some one please help....
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
Apparently it was originally done for efficiency:

"Why is each public class in a separate file?
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. "
-- http://www.roseindia.net/javatutorials/why_is_each_public_class_in_a_separate_file.shtml


Matt
Inquisition: open-source mock exam simulator for SCJP and SCWCD
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2343

Waseem,

It's your java platform that decides whether this is enforced or not.
You can read more in this post.

Regards, Jan


OCUP UML fundamental
ITIL foundation
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

"But that is so irritating. Why is it so strictly compulsory..??"

Actually, I think it is a very good restriction, because it forces you to keep your source files organised so that it is very easy to find the source file that a particular public class or interface is implemented in.

Have you ever tried to figure out the source code of a medium or large C++ program? You'll spend a lot of time searching for the declaration and definition of classes. Because there is no restriction like in Java it is much harder to find where stuff is and to get an overview of what classes exist in the program and how they are separated into modules.

Because Java source files are organized with one public class or interface per file and because the directory structure must follow the package structure, it is very easy in Java to see the structure of the program just by looking at which directories and source files exist.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
waseem nadaf
Greenhorn

Joined: Mar 28, 2007
Posts: 10
Thank you so much Jan Cumps, Jesper Young and Matt Russell... your replies were a great help... my doubt is cleared...
Ajaykumar Yavagal
Greenhorn

Joined: Apr 30, 2007
Posts: 1
Hello Waseem,

What you have asked is not actually the JAVA language requirement, but rather.. an implementation requirement of many compilers(including compilers from Sun Microsystems).It's therefore not advisable to ignore this convention, because doing so limits the portability of your source files {but not, of course,your compiled files}. Hope this answers your question
[ April 30, 2007: Message edited by: Ajaykumar Yavagal ]
Arad Chear
Ranch Hand

Joined: Jan 05, 2007
Posts: 98
one of the best features in java is package-Centric

when you want something you must access it through package

and from package you can find list of public classes ,

thats whats make Java API very wonderful and very effeicient ,

imagine when you have more than public class ber source file ,

its a bad idea , if you want more than one public class you must create package for them ,

by the way you can define public class (inside) public class
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56204
    
  13

"Ajaykumar",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: only one PUBLIC CLASS per source file..... But Why..?????
 
Similar Threads
URGENT
Public Classes
Rules regarding Public Classes
public class
Confused on example in chapter 2 of "Head First Java"