Yes, in Java we can declare the main method as private.
It can be compiled successfully without any errors, but at runtime it will prompt that the main method is not public.
class PrivateMainMethod { private static void main(String args[]){ System.out.println("Welcome to Tutorials Point"); } }
The above code runs successfully at compile time, but throws an error at runtime.
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 javafx.application.Application
The above is the detailed content of In Java, can we declare main method as private?. For more information, please follow other related articles on the PHP Chinese website!