I have a java file containing more than one class, out of which one is public. If main method is inside a non-public class. I can't run that java file. Why is that? and there is no compilation error as well. If so, how can I use that main method?
问题:
回答1:
You can certainly override main method and it does not violate any compiler rules and hence you will not have any compiler errors.
You check that inspite of the fact that you have more than one class a file that is declared as public is the name of the file you are trying to execute.
This is a convention that the file should be named after the same class which is public in that code.
Hence when you try to execute that class it does not have a main method from which it starts execution.So if you want to execute the main method in the non public class the only way to this is call that main from a main method of the public class.
回答2:
Actually you can execute the main method in a non-public class. if you put this class
class A {
public static void main(String... args) {
System.out.println("This is not a public class!");
}
}
in a file named NonPubClass.java. You can compile this file using javac command but you will not get a NonPubClass.class, you will get a A.class instead. Use java a to invoke that class and you will see the printed string --- This is not a public class!
回答3:
Have a look at this code:
Super.java
public class Super{ }
class Sub{
public static void main(String[] s){
System.out.println("Hello");
}
}
In order to print Hello
you can compile and run the program as:
How this works?
The compiler generates separate .class
file for every class in your program. So, instead of calling the main()
of non-public class from the public class's main()
you can print the output as shown above.
Note: As the convention says, you must put a public class
in separate file <class_name>.java
. And do not put more than one class in a single file (except if they are inner class) because if you would like to import them or use them with other classes then it will cause problem.
回答4:
Simple Answer. You can't
. You need to have main method in a public class
and its signature should be public static void main(String... args)
there is no compilation error
Why there would be? You are doing nothing wrong as far as compilation rules are concerned.
Only thing is that your non-public-class-main-method
won't work as an entry point of your code.
回答5:
there is something i would like to add although everybody here believes that a public is necessary for the main in a class and that it won't work without main
you can have as many mains in a class as you desire, and you can have them without a public access modifier. but be careful, only that class which is named after the file can be public what i mean is if you name your file a.java , then only the class with name a can be public, none other can have this facility
here is a code to show this : as you can see the name of the file is helping.java
//:initialization/helping.java
class b{
public static void main(){
System.out.println("hello its b");
}
}
class helping {
static void f(float i, Character... c) {
System.out.println("first");
}
static void f(char a, Character... args) {
System.out.println("second");
}
public static void main(String[] args) {
f(1,'a');
f('a','b');
c.main();
}
}
class c{
public static void main(){
System.out.println("hello its b");
}
}
//:~
/*
* output:
* first
* second
* hello its b
* */
回答6:
It is a compile-time error if a top level type declaration contains any one of the following access modifiers: protected, private, or static.This link may be helpful.
回答7:
It's not a compile time error as u mentioned that top level type declaration shouldn't be protected, static or private.
If u go through the link http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.6 well that u have shared ,then it's quite clear there that a top-level type declaration refers to only "top level Class and Interface type declarations" and these should not be protected, static or private at top level declarations, but we can use protected, static or private for any methods or variable declaration inside them.
With respect to above code, there is nothing wrong in declaration, and the code will compile and run successfully as all outer top level class are default and there is no violation.
The answer to the question asked at top is exactly as mentioned by few experts at top, that
"for sure we can have a file with main method inside non-public class and it will compile as well as run successfully, but make sure that at the time of running the program we have to pass the class name of "main method" to the java interpreter instead of the class which is public."
If we have 2 classes A(public) and B(non-public containing main method) , then the file will compile with "javac A.java" but while running the code we need to pass the command as "java B" .