3

in java, filename should be same as that of main class. It is the way of telling compiler that this is the entry point for you. but why this thing works:

class xyz{
public static void main(String[] args){
System.out.println("a");
}
}

even when saved with different filename.

And why this thing does not when saved with diffrent filename:

public class xyz{
public static void main(String[] args){
System.out.println("a");
}
}
Moose
  • 751
  • 22
  • 42
  • 5
    The classname == filename rule only applies to `public` classes. See http://stackoverflow.com/questions/2134784/why-filename-in-java-should-be-same-as-class-name for more information. – azurefrog Sep 18 '14 at 18:33
  • 2
    Also, classes might be *generated* on the fly, and not correspond to any file at all. Or the class might be downloaded from a network, or gotten any other place which might not be a file on disc. – markspace Sep 18 '14 at 18:35
  • good question - the specification of the jvm says nothing about naming conventions; probably just a choice of common sense all implementations agreed on - though i'd say it's the other way around: the file name has to match the class name – Seismoid Sep 18 '14 at 19:07
  • if i compile it with filename and then run it by calling my classname having main function, it is working, does that suggest that filename is not necessarily same as class name ? also does that means that i need not have a public class in my code necessarily ? – Moose Sep 18 '14 at 19:14

2 Answers2

10

public classes have to be in a file with the correct filename. Non-public classes can be in any file you want. Even multiple classes in the same file if it is convenient.

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • That i know, but why is it so ? Any particular reason, why it was done this way at the first place ? – Moose Sep 18 '14 at 18:35
  • 1
    http://stackoverflow.com/questions/2134784/why-filename-in-java-should-be-same-as-class-name – khelwood Sep 18 '14 at 18:36
  • I have already gone through this post. My doubt is even non-public classes can have main method, so wouldn't that pose a problem to jvm, when it is searching for an entry point ? – Moose Sep 18 '14 at 18:42
  • I think it is a "good practice" choice more than anything. The language designers wanted to prevent people having multiple public classes in the same file. – khelwood Sep 18 '14 at 18:44
  • Sun introduced this, so that jvm does not have to spend time of searching for an entry point, but lets say my program has only non public classes, and one of them has a main function so wouldnt it be a problem then ? – Moose Sep 18 '14 at 18:46
  • The jvm isn't going to be looking at .java files. By the time the jvm gets to it, it's all in .class files, so that can't be the reason. – khelwood Sep 18 '14 at 18:48
  • the link you posted says so. my doubt is simple "non public classes dont need same file name, why ?" – Moose Sep 18 '14 at 18:57
2

Note that:

class xyz

Is not a public class so it cannot be acessed from outside of the file. Therefor it does not need to have the same name. But in this case:

public class xyz

You do have a public classe, that it gonna be acessed from outside of the file, so it does need to have the same name.

Conclusion: public classes need to have the file name exatly the same as the class.

AndreDuarte
  • 784
  • 1
  • 5
  • 21
  • Sun introduced this, so that jvm does not have to spend time of searching for an entry point, but lets say my program has only non public classes, and one of them has a main function so wouldnt it be a problem then ? – Moose Sep 18 '14 at 18:43
  • I dont think that this is gonna work. You wont be able to call you Main method within this non-public class – AndreDuarte Sep 18 '14 at 19:00
  • 1
    i wish i could show you. class xyz{ public static void main(String[] args){ System.out.println("a"); } }.. this runs – Moose Sep 18 '14 at 19:01
  • How are you running this? Terminal? Show me the command – AndreDuarte Sep 18 '14 at 19:06
  • java xyz. by calling classname. – Moose Sep 18 '14 at 19:07
  • check this [link](http://stackoverflow.com/questions/12524322/what-if-main-method-is-inside-non-public-class-of-java-file) – AndreDuarte Sep 18 '14 at 19:07
  • 1
    link you gave me has an answer, which says it will work if you compile it with filename and then call it with class name – Moose Sep 18 '14 at 19:12
  • Yep, but even if it works, it's not a good idea :-) – AndreDuarte Sep 18 '14 at 19:13