Can we declare the main method as private in Java?

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 MainMethod {
   private static void main(String args[]){
       System.out.println("Welcome to Main Method);
    }
}

 

Output:

Error: Main method not found in class MainMethod , please define the main
method as:
public static void main(String[] args)

 


Flutter Questions

Android Questions

Python Questions

Java Questions