• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic