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?
-
2Main method must be able to access from outside. – JSPDeveloper01 Sep 21 '12 at 04:46
7 Answers
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!

- 786
- 7
- 10
-
8+1. Why is everyone else saying that you can't run the main method of a non-public class? Maybe older JVMs did check the entry class's public flag? My JVM (HotSpot 1.7) doesn't seem to care. – Daniel Lubarov Oct 28 '13 at 01:52
-
@charles_ma you could've named your class as `NonPubClass` instead of `A` – divine Nov 20 '19 at 01:34
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.

- 5,783
- 15
- 70
- 117
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,792
- 8
- 50
- 57

- 101
- 5
-
A misleading statement: "you can have as many mains in a class as you desire" - you mean as many mains in a single Java source file. You can only have one main in each class. Your file contains three classes. – Klitos Kyriacou May 12 '17 at 09:15
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.

- 12,895
- 18
- 59
- 82
-
1That's correct. To complement the answer to the OP question, it makes sense not having compilation errors since he is creating a method named main and this is valid for the compiler, but if the method is no t within a public class, it will not run when the program launches. – Hernan Velasquez Sep 21 '12 at 04:46
-
-
4
-
14Since you're still active, can you please remove this answer? It is not correct that the `main` method needs to be in a `public` class to be usable and may distract unexperienced Java users, who don't know it better. (If that statement was true on the time it was posted, than please note, that this isn't the case anymore and this answer should at least be updated). Thank you. – Tom Feb 07 '16 at 14:18
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" .
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.

- 474
- 2
- 9
- 21
-
7
-
5The answer seems incorrect. First as pointed out by Lew Bloch, you can't override a static method and second, you can actually launch the main method in the non-public class without going though the public class as suggested here. You have to pass the name of the non-public class, which contains the "main" method, as argument to the "java" command. – KRiSHNA Mar 03 '15 at 19:25