java – Whats the difference between main() and main(String args[])

java – Whats the difference between main() and main(String args[])

public static void main(String[] args is the entry point (which can be final or not, doesnt matter) that the java tool and standard IDEs and such look for in the main class of a Java application. If you dont include the parameter declaration (the String[] args), the signature doesnt match the expectation of the java tool and so may not work.

main() will compile, because its just a method, but wont work with the java tool and other tools following its conventions.

If BlueJ allows you to leave off the parameter declaration, thats behavior specific to the BlueJ tool.

So for instance, this compiles just fine:

public class Example {
    public static void main() {
        System.out.println(Hi);
    }
}

It compiles to an Example class with a method called main. But if you try to run that via the java tool:

$ java Example
Error: Main method not found in class Example, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

To make it compatible with the java tool, you need the parameter.

java – Whats the difference between main() and main(String args[])

Leave a Reply

Your email address will not be published. Required fields are marked *