I am confused about file names , in the above code , if i keep the class name as nstack.java , the compiler says that class stack is public should be declared in a file name called stack.java ,
now if i give the file name as stack.java then it compiles just fine but but when i try 2 run it it gives error " Exception in thread " man" java.langNoSuchMethodError: main
also i wanted to know what does this statement mean ...
* Files with no public classes have no naming restrictions.
if the above is true then
will the above code work if the File name is bool.java ???( i have noticed that when i compile it it gives out bool1.class which is normal , now if thats the case isnt the statemnet ( * files with no public classes have no naming restrictions) contradicted ?
I'm not sure what you're confused about. I can tell you that there are three unrelated things going on here.
1) When you compile any Java source file (a *.java file is called a "source file"), a class file is created for each class in the file. Each class file is named X.class, where X is the name of a class as given in the source file.
2) There's a rule that there can be only one public class in a source file, and if there is a public class in a source file, the source file must have the same name as that class, plus ".java".
3) When you run a Java application, the argument to "java" is the name of a class which contains a "main()" method: this name has nothing to do with the name of any source file, but only to do with the name of a class.
i guess i was not descriptive about my problem....
well in the stack program , there is one public class , so according to rule the name of the file must be the name of that class , now if i do that it compiles fine but since the class in which the main method is there has the name nstack so wen i try to Run the program it gives me error saying it did not find any MAIN method ,
so my question is , i s there anyway this program will run ... or the only possible way is to make another file and put the public class ( stack class) in that file and name the file as stack.java ....
hope i was clear , thanks for ur reply , would really appreciate further clarifications ,
As I tried to point out, all of these things are really unrelated. You tell the compiler (javac.exe) the name of the source file, and it creates some number of class files. Then you tell the runtime (java.exe) the name of the class you want to run, the one with main() in it. So in your case, you'd say