Just one public class per file!!

Hello all,
This time i have got a simple but confusing(for me) question.
I know that in a file only one public class  is allowed ,the question is why they have put such a rule?
-------
suppose i have got three "public" classes written in 3 diff files(say j1.java,j2.java,j3.java)When i compile j1,j2,j3 indivdually i will get three class files j1.class, j2.class and j3.class
--------
now i am putting these three java files in a single file say j.java ,and when i compile j.java, still you will get those j1.class, j2.class and j3.class(+j.java). what is the difference between these classes and the above classes?
----------
i refered all the java sites that i know,but all just say that ,only one public class/file because sun desiged it that way..
--------
ooh man !!somebody teach me the fundamentals :(

With regards  
Priyan.T
icq:54490947
priyan_t@yahoo.com

PriyanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PriyanAuthor Commented:
Edited text of question.
0
JodCommented:
Every class in your code becomes a class file - even inner classes like you have for example.

No matter how you organise your code into java files, the appropriate class files are ALWAYS created. This is just the way Java works.

If you want to know more then look here...

http://codeguru.developer.com/java/tij/tij0081.shtml
0
heyhey_Commented:
you should be able to find the source for EVERY PUBLIC class inside the .java file with the same name.

non-public classes inside the same .java file are supposed to be 'helper classes' that are not used outside this .java file.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

imladrisCommented:
And the reason is, that the JVM needs to be able to find them too. Since the class files are not precombined into a single executable program, the JVM has to be able to find the file associated with a public class, while it is executing a program. The surest way of doing this is to require the class file to have the name of the class it is looking for. This way the JVM can check along the directories of its class path for the relevant file, then load it, being quite sure that the file contains what it is looking for.
0
heyhey_Commented:
hmm, are you sure that JavaVM (not java compiler) needs the source files  ? :)
0
feloniusCommented:
As far as I know it has something to do with readability of the code - besides of course imladris comment above.

When you grab some code that somebody else has written and you are unsure what to do with it (maybe because of bad documentation) you can always look inside the directory to see what classes that are supported for public use. That means that it is easier to determine what to do. If all .java files just made 1 .class file each one would have to look though the files to determine what was useful.
0
heyhey_Commented:
felonius: why did you post an answer (and why do you think everybody posts comments ?)
0
feloniusCommented:
1. Because Priyan had accepted none of the other comments as answers yet, and a lot of days has gone by.
2. Because I think my "comment" might be worthy of being called a acceptable answer. If that is not the use of the "answer"-feature of experts-exchange then want is the point in having answers in the first place and not just acceptance of comments?
0
JodCommented:
I have to be honest - one of the biggest problems with managing large Java projects is the hundreds of .java files that you end up scattering all over your disk, some of them in very deep and obscure directory structures.

Certainly doesn't make the code easier to work with.

In my own personal opinion I thought it was to allow the code to be packaged into discreet chunks. Java is a network programming language which allows for late and dynamic binding to actual bytecode. This means you want to package chunks of code into small packages that can be downloaded across a network quickly.

This means that if you change one class file you do not need to change any others and more importantly the user does not have to download any classes other than the ones that are changed. The JVM will rebind them with the older code at runtime.

This one of the main advantages of having bytecode in small class files.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
feloniusCommented:
felonius changed their proposed answer to a comment
0
JodCommented:
Don't worry, felonius - happens to us all...

http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=10264549 
0
PriyanAuthor Commented:
hello every one,
Sorry for not responding to your valuble comments,jod, thanks for your url ,that you had posted.It really helped me a lot.Thanx once again for all others who answered this question.
With regards
Priyan.T
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.