DEV Community

Calin Baenen
Calin Baenen

Posted on

Can `main` be `protected` (and be the program entry point) in Java?

Top comments (3)

Collapse
 
habereder profile image
Raphael Habereder • Edited

To quote Oracle:

The method main must be declared public, static, and void. 
It must specify a formal parameter whose declared type is array of String. 
Enter fullscreen mode Exit fullscreen mode

Which can be found in the official Java Language Specification

Collapse
 
pgradot profile image
Pierre Gradot

No. Period.

Code:

public class HelloWorld{

     protected static void main(String []args){
        System.out.println("Hello World");
     }
}
Enter fullscreen mode Exit fullscreen mode

Output:

$javac HelloWorld.java
$java -Xmx128M -Xms16M HelloWorld
Error: Main method not found in class HelloWorld, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Enter fullscreen mode Exit fullscreen mode
Collapse
 
eelstork profile image
Tea

Try it!?