Is the String args [] parameter required for programs that do not require command line arguments?

As far as I know, it String args[]

takes an array of type elements String

- the mechanism through which the runtime passes information to the application.

If we take a simple addition program:

class Add {

    public static void main(String args[]) {
        int x = 10;
        int y = 30;
        int c = x + y;
        System.out.println(c);
    }
}

      

Obviously, the program doesn't need command line arguments to calculate the result. No values ​​are passed to the args array. So, do we need to include this array, or main()

does the syntax require us to do it differently?

+3


source to share


6 answers


you need to have a named function public static void main(String[] args)

as the entry point to your Java program.

If you look at the documentation for the actual java command , it makes it explicit:



The java command starts the Java application. This is done by launching the Java Runtime Environment (JRE), loading the specified class, and calling that method's main () method. The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter. The method declaration looks like this:

public static void main(String[] args)

      

There is a difference between this and the JavaFX program; its starting point of entry is in the function start()

.

+4


source


Yes - if you leave the arguments, you will get the following error:

Error: main method not found in class nl.magnus.test.Test, please define main method as: public static void main (String [] args)



This requirement is main

specified by Oracle in the Java SE specs :

Finally, after initialization for the Test class is complete (during which other subsequent loading, binding, and initialization may have occurred), the test main method is called.

The main method must be declared public, static and void. It must specify a formal parameter (Β§8.4.1) whose declared type is a String array. Therefore, one of the following declarations is valid:

public static void main (String [] args)

public static void main (String ... args)

+2


source


The short answer is yes. Your method main

must have a parameter String[]

, even if it doesn't use one.

If it does not have such a parameter, Java simply treats it as any other method public static

, not as a program entry point.

+2


source


From The Java Tutorials :

In the Java programming language, every application must contain a main method, the signature of which is:

public static void main (String [] args)

+1


source


This is the required definition of the entry point for a Java program. You must have it.

0


source


Its method syntax main()

, whether you are sending the current arguments or not from the command line.

The execution of any program will start with just this signature main()

: -

public static void main(String args[])

      

0


source







All Articles