Home  >  Article  >  Java  >  In Java, can we declare main method as private?

In Java, can we declare main method as private?

王林
王林forward
2023-08-22 21:37:061016browse

In Java, can we declare main method as private?

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.

Example:

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.

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 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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete