DEV Community

loading...

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

baenencalin profile image Calin Baenen ・1 min read

Discussion

pic
Editor guide
Collapse
habereder profile image
Raphael Habereder

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