vijay arora
Have the determination of mirror which never fails to reflect in spite of being broken into pieces.<br /> <br />Kiss the hands you cannot bite.<br /> <br />An Optimist is one who starts taking a bath when he accidentally falls into the water.
SCJP, SCWCD.
|Asking Good Questions|
vijay arora
Originally posted by Viju:
Now, compile the same code after removing "public" before the class name, this time the code will compile and run without any error.
but my question is WHY?
What is the logic behind this?
Please!Originally posted by Vishal Pandya:
'I' think there is no logic behind it.It's simply a rule.
Imagine that you have two files, A.java and B.java containing classes A and B. Furthermore, imagine that class A mentions class B. Now, you type "javac A.java". At some point, the compiler is going to look for B.class, and since it doesn't exist yet, what should the compiler do? Of course, what it does is look for "B.java" and expect class B to be defined in it. If B.java instead contained class C, and C.java contained class B, then the compiler would fail to compile anything.
So even without the rule about one public class per file, you can see another common sense rule: if a class is referenced outside of the source file in which it is defined, the source file should be named after the class. Many Java compilers will warn about violations of this even for non-public classes.
Now, the only reason for a class to be public is for it to be used outside of its own source file, right? So it makes sense to make the rule stronger in this case. The common-sense rule, the one that people follow consciously or not, is that a source file should contain at most one class that is ever mentioned by name outside of the file, and the file should be named after that one class.
vijay arora
You're welcome.Originally posted by Vijay Arora:
Thanks Campbell for the hint..searched through the forum and found the above information.
vijay arora
Thank you very much.Originally posted by Vijay Arora:
Ya sure..following is the url where i found the above quote
Have you tried it?Payel Bera wrote:Hi Campbell, Thank you for your clarification!!
1) If no public class, then the name of the java file can be the same as any of the non public class or any other name like abc.java,,, please let me know if my understanding is correct.
No. You can't run a .java file. You can only interpret .class files.
2) if we have class sample in file sample1.java then the code inside sample.java will execute whenn we run sample1.java,,, please let me know if my understanding is correct. Thanks in advance,.
Consider Paul's rocket mass heater. |