• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
waseem nadaf
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jan Cumps
Bartender
Posts: 2632
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Waseem,

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

Regards, Jan
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
waseem nadaf
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Jan Cumps, Jesper Young and Matt Russell... your replies were a great help... my doubt is cleared...
 
Ajaykumar Yavagal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!