Home > Java > javaTutorial > body text

In Java, can we declare main method as private?

王林
Release: 2023-08-22 21:37:06
forward
1047 people have browsed it

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");
    }
}
Copy after login

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
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!