wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question about the java programming rule! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A question about the java programming rule!" Watch "A question about the java programming rule!" New topic
Author

A question about the java programming rule!

Daniel Washington
Ranch Hand

Joined: Oct 26, 2002
Posts: 53
why should Java only allow ONE public class in a java source file ,and if the class is declared "public",why the filename should the same with the public class name?
Is it true: in our programming ,we only use the .class file ,not the .java file,so it is not important about the rule on .java.But it seems Sun doesn't think so.
every useful answers are welcome.thanks a lot!


IBM 141<br />IBM 285,286<br />IBM 700
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
This goes back to Oak was done to make it easier for the compiler to find and compile dependent classes. In other words, suppose while compiling your program the compiler finds a reference to a class named FindMe. Assuming there is no class file named FindMe.class, the compiler will try to find it in order to compile it for you. But what should it look for? It will look for FindMe.java.
So the only reason is so that the compiler can find and compile dependent classes without having to search through every .java file.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Well according to me the public class name should be the name of the java file is not a compiler rule though Sun recommends it. It is up to the compiler to enforce the rule or not. That is, a compiler developer can disregard this rule. Well I think that its really useful to keep the class name as the name of the file as in case someone wants to know what a specific file does or wants to look what methods does a class have it becomes much easier. I find it better to keep non public java files in their class named file as well.
[ May 16, 2003: Message edited by: Anupam Sinha ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088

But another scene is :when there is a "public" class in a java file, then you must create the "main" method in this class,not the other "non-public"class.So why? I think the position of "main" method doesn't matter with the access control modifiers!

This is not true. Consider this :

Compile it and name it test.java. As test is a public class. Compile the file. It will create two files test.class and test1.class. The command line java is used to start a class's main() maethod. So when you do java test it will give you an error because it doesn't has a main method. Instead you should do java test1 which would then execute the required file. If you want both the classes can have a main method and you can decide which one you want to execute.
Daniel Washington
Ranch Hand

Joined: Oct 26, 2002
Posts: 53
Ok,thanks anyway,I already got your point!
But why two "Public" class are not allowed in ONE
java file?I think it should not matter with the
access modifier"public".
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Daniel Washington:
But why two "Public" class are not allowed in ONE java file?
Did you read my post above?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question about the java programming rule!