DEV Community

Discussion on: Can the class containing `main` be private?

 
stefandrl profile image
Stefan Drl • Edited

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
Calin Baenen

Thank you.