Link to home
Start Free TrialLog in
Avatar of Priyan
Priyan

asked on

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

Avatar of Priyan
Priyan

ASKER

Edited text of question.
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
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.
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.
hmm, are you sure that JavaVM (not java compiler) needs the source files  ? :)
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.
felonius: why did you post an answer (and why do you think everybody posts comments ?)
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?
ASKER CERTIFIED SOLUTION
Avatar of Jod
Jod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
felonius changed their proposed answer to a comment
Don't worry, felonius - happens to us all...

https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=10264549 
Avatar of Priyan

ASKER

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