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

Asked

Viewed 3,113 times

33

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

  • This beginning of Java means. The Method that should be started first is public, static, returns nothing is the user can pass arguments as string "Character set"

  • It does not answer your question completely, but it may help you to understand more about this entry point: http://answall.com/a/93051/41056

  • 3
  • 1

    Just one reading on both and you can see that the subject is the same, but the question is not the same, therefore it is not duplicate. We cannot close questions just because the subject is the same, that would prevent most of the questions already asked. Actually looking at the answers you have there do not answer what was asked here, so it makes no sense to close this.

3 answers

31


It’s mandatory because every app needs a point of entry. Normally 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.

In fact you also need to specify which class will be the main one since the method main() may exist in other classes.

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

It is not always mandatory

There are some situations where it is not required. It is the case of web applications that already start somewhere else from 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 function with only one static method within a class considered to be the main one. Nor do I see a general advantage in that, but it is possible according to a response in the OS.

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

I put in the Github for future reference.

Signing

In the signature of the method was agreed to the use of 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 has also been decided to return nothing to the operating system, unlike other languages that do. You can ignore this variable.

Unlike other Java languages it 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 actually a simplification.

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

  • 1

    Look, the answer is excellent, only I was confused if AP cares about main only or also wants to know the arguments (although he did not mention this). However I leave the +1

  • Great answer, but what is the purpose of passing the "String[] args" as parameter ?

  • @Josevieiraneto the purpose is that "args" is a string array that contains the parameters passed at the time of execution. See example: http://www.devmedia.com.br/understandndo-o-parametro-string-args-em-java/29245

7

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 have already tested it, and I know how it works.

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

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

I do not know if in all languages are like this, but in all that I have worked until today (Python, PHP and the like), the first argument that is 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.

0

Basically this method means that at you are making the class you are implementing the main.

It is he who makes the execution of all the code you build or calls-Ló.

  • What would a main class be? I believe you are referring to the input method only. The class plays no role pro compiler, but the input method.

  • 1

    @Cypherpotato, in this case is neither pro compiler, but rather for the execution time you need to inform which class will be executed. Compiler lives well without this method in any class

Browser other questions tagged

You are not signed in. Login or sign up in order to post.