Actually this is not necessary..!!! but it is a good programing practice if you do so..!! I tell how it works:
Suppose you have one java file name : Hi.java inside this you have following code :
If you compile this code, this will compile fine..But after compilation..it generate a class file..The name of class file is decided by compiler..and compiler pick the same name as that of parent class in the java file ..so after compilation the class file generated will be : Hello.class
So in order to avoid confusion..it is always a good practice to keep source file name as that of parent class name.
Name of java file should be same as the public class present inside it.
I highlighted the word "public", since you CAN have a different name as long as all classes have default access (i.e. no public, protected or private). As soon as there is one public class (and there can be only one per file), the file name MUST be the same as that class name.
i agree with the above point that we can define n number of non public classes in a file.but what if i define some around three non public classes and all the three classes containing main method and each class givin cal to other class's main method. check this link and explain me please.