• Post Reply Bookmark Topic Watch Topic
  • New Topic

The Source file  RSS feed

 
Bindu Kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two top level classes cannot be in the same source file. Why is it like this ?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not true. Two public top-level classes cannot be in the same file. And, if there is a public top-level class, then the file must have the same name as the class.

The simple reason is "because that's what the Java Language Specification says". In my view, it is not particularly beneficial for beginners to try to analyse every decision the language designers made.

In this case, I believe the motivation for the decision was to make it easier to find the file that contains a particular class. Compare Java, which has this rule, to C++, which has no such rule. In C++, classes and functions can be in any file, anywhere, and one usually has to resort to grep to find them.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!