aspose file tools
The moose likes Beginning Java and the fly likes class name and source file name Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "class name and source file name" Watch "class name and source file name" New topic
Author

class name and source file name

ck kanwar
Greenhorn

Joined: May 07, 2003
Posts: 3
I read it is mandatory to have the same name for the public class as the source filename for the program to compile. I also read it somewhere that it is a compiler constraint to have the same name at both the places. Could someone please give me an insight into it. Why exactly same name is needed, is it tht the compiler searches for a class with the same name for the starting point of the file?
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 973
That was a good Q...why is that there should not be two classes that are public be declared in the same file;whereas it is fine if one is public and other not?

Thx
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
As the file must have the same name as the public class contained in it, it's impossible to have more than one public class in a file.
If you put only non-public classes in a file there's no restrictions to filenaming.

It is a good practice to not have more than one class per file though (not counting inner classes) and to always give the file the name of the class. Makes it so much easier to find the right files when you need to change a class

It's a requirement laid down in the JLS and/or JVMS. Those form the bible which determine the behaviour of the JVM and compiler. As gospel they don't have to be logical or reasonable (though by and large they are) and no explanation of their reasoning need be given.


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class name and source file name
 
Similar Threads
What will be the name of .java file?
cannot read
Search a directory
how to invoke a java program using shellscript
ClassNames