• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class name and File name in Java  RSS feed

 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering why should the class name and the file name should be the same in Java.
Is it because it makes it easier to organize and maintain your code? (I actually don't understand how it makes it easier to maintain or organize code I just read it in a text book :p)
Thanks in advance for any help!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody knows. I've once read somewhere that it was done to make the compiler "more efficient". Probably it's also to make people organize their code better. There's no strictly technical reason why it has to be like this, it's just a choice that the original designers of the Java language made.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're looking for the source code for a class named DataFinder, it helps an awful lot if you know it's in a class named "DataFinder.java". I was once working with some open-source code which used the option of storing non-public classes in a source file with a different name and it was very frustrating trying to find where the source for that non-public class was stored.

When you're writing code, you may not think it's an issue if you put your source code in any old place. But when somebody else has to work with that code (say, you six months later) it's definitely an issue.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have 2 classes -- yeah, it's easy to find them. When your project gets to thousands of classes, you'll appreciate it more.
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I get it now! Thanks for the help! I appreciate it!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!