Why is it mandatory to implement " public static void main (String [] args)"?

Why is it mandatory to implement this method in a Java application?

Author: Maniero, 2016-09-05

3 answers

Is required because every application needs an entry point. Usually the operating system needs to know where the code starts, in Java it is actually its operating environment, it is the JRE that will start the application and needs to know what to call first. it was agreed that this method would be called main(), since other languages used something like this.

Actually also needs to specify which class will be the main one since the main() method can exist in other class.

The body can be as simple as just calling another method. I see no use in this, except for some very specific purpose, other people do without knowing why.

Is not always required

There are some situations where it is not mandatory. This is the case of web applications that already start somewhere else in the framework used and then go calling what you programmed. In practice there is a main(), but not in your code.

It is also possible to work with only one static method within a class considered primary. I also don't see general advantage in this, but it is possible according to a answer in the OS.

class JavaAppWithoutMain {
    static {
        System.out.println("Hello World!");
    }
}

I put on GitHub for future reference.

Signature

In the method signature it was agreed to use String[] args to receive command line arguments-as they can be multiple and can contain any type of information the ideal would be a array of string. It was also chosen to return nothing to the operating system, unlike other languages that do. You can ignore this variable.

Unlike other Java languages preferred not to give options and this is the only possible signature. The philosophy of language is to simplify, although it is debatable whether this is a simplification in fact.

There are more details about each component in what it means public static void main (String[] args)?.

 31
Author: Maniero, 2020-11-06 17:02:51

Generally, in any programming languages, this parameter represents the input data that will be passed to your program.

I don't have much understanding about Java, although I've already tested this, and I know how it works.

If you for example have a class called Hello that has this String args[] as parameters, when you run the command java Hello world

The result of this String args[] will be ["Hello", "world"].

I don't know if in all languages they are like this, but in all that I have worked until today (Python, PHP and the like), the first argument that is about the name of the program (in my example is "Hello") is always present.

This is very useful for you to pass arguments to your application.

 7
Author: Wallace Maxters, 2020-02-21 13:33:16

Basically this method means that na You are making the class you are implementing as main.

Is it that does the execution of all the code you build or call lot.

 -1
Author: Vanessa Nunes, 2016-09-20 22:39:00