File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Why the name of the public class should be the name of the file as well Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Why the name of the public class should be the name of the file as well

Avinash Haridasu
Greenhorn

Joined: Jul 12, 2011
Posts: 25
In Java,

When a file contains public class

public class Demo{
}

The code above should be saved with a file name Demo.java


Why it does not get compiled if the file name is not Demo.java ??
Is there any reason for that ??
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3050
    
    1

Yes. This is so the compiler can easily find files when a class from outside the package refers to it. And not only the compiler: imagine how annoying it would be when you're looking for the source code of some class, so you can understand the implementation. You would have to open every file in the package to find the class, or write a program to look for it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
If you search, you will find many threads about the same question.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Why the name of the public class should be the name of the file as well
 
Similar Threads
Java source code File name
two public classes in a file
stange problem
ClassNames
public class