DEV Community

loading...

Can the class containing `main` be private?

baenencalin profile image Calin Baenen ・1 min read

I realize, that, in some cases, main shall not be private (and sometimes not protected), and in other cases, it may compile.

But, I was wondering if the class containing main can look like this:

private final class Main {
    public static void main(String[] args) { /* ... */ }
}
Enter fullscreen mode Exit fullscreen mode

.

Discussion

pic
Editor guide
Collapse
stefandurlesteanu profile image
Stefan Durlesteanu

Hi! I'm not an expert, but I've never seen a private Main class. In my experience it is always 'public static void main'. Also, I've found this short and logic explanation.

Collapse
baenencalin profile image
Calin Baenen Author

Thanks for the recommendation you provided, but, for some reason, when I try to open it, it just closes, as if window.close() was invoked(??).

Collapse
stefandurlesteanu profile image
Stefan Durlesteanu

Hmm, works fine for me. Here is the raw link to the source (tutorialspoint.com/can-we-declare-....)

Thread Thread
baenencalin profile image
Calin Baenen Author

Weird. Whenever I click on links to this website, it brings me to the website homepage (index.htm), when I right click, and try to use one of the Open in ... options, it brings me to a smartredirect.de URL, specifically; this one.

Thread Thread
stefandurlesteanu profile image
Stefan Durlesteanu

Let me just write it down here for you, there is no that much content.

Yes, we can declare the main method as private in Java.

It compiles successfully without any errors but at the runtime, it says that the main method is not public.

class PrivateMainMethod {
   private static void main(String args[]){
       System.out.println("Welcome to Tutorials Point");
    }
}
Enter fullscreen mode Exit fullscreen mode

The above code is working successfully at compile time but it will throw an error at the runtime.

Output:

Error: Main method not found in class PrivateMainMethod, 
please define the main
method as:
public static void main(String[] args)
or a JavaFX application class must extend j
avafx.application.Application
Enter fullscreen mode Exit fullscreen mode
Thread Thread
baenencalin profile image
Collapse
cyberhck profile image
Nishchal Gautam

Yes, you can make it private, but your JRE (runtime) calls YourJar.main() which isn't available and will fail (its like what happens when you try to invoke a private method) I think it should have exact same error message (because main is called on runtime, compiler doesn't complain, because no one is calling that method, yet)

Collapse
baenencalin profile image
Calin Baenen Author

Ol, wb the class containing main inside it?

Collapse
cyberhck profile image
Nishchal Gautam

I'm not sure if I follow,
basically your java vm will simply call your class.main method, and when it finds that it's private or doesn't even exist, then it'll throw up

Thread Thread
baenencalin profile image
Collapse
thefern profile image
Fernando B πŸš€

Why not just make main private and run it and see what happens.