• Post Reply Bookmark Topic Watch Topic
  • New Topic

class name and source file name  RSS feed

 
ck kanwar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!