What does public static void main (String [] args) mean?

26

I want to understand what each item of public static void main(String[] args) means in Java and when it should be used.

    
asked by Tiago Oliveira de Freitas 18.10.2015 в 02:10
source

2 answers

33

public

This is the method access modifier. Using this modifier the method can be accessed by any class inside (and outside) of the project.

Other modifiers are protected , private or no modifier *. Here you can read more about the Java access modifiers.

static

Set the method to static , this means that the class does not need to be instantiated to invoke this method.

In the example, I have the class Cliente with the methods (static) FazerAlgo() and (non-static) FazerAlgoDois() , the usage would be like this:

Cliente cliente = new Cliente();
cliente.FazerAlgoDois(); // Este é o método não-estático

Cliente.FazerAlgo(); // Este é o método estático

void

It is type return method. This type of return means empty / nothing, the method gives no return. Methods can return any type of your project, even the ones created by you.

main

This is the name of the method. Every method must have a name. The names are defined by the programmer and generally follow a conventional pattern previously defined by the language or community, although this is optional. In the case of Java, Oracle itself defines these conventions. The convention naming methods says :

  

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.

Free translation:

  

Methods must be verbs, in mixed case with the first lowercase letter and the first letter of the words capitalized.

In Java (and other languages too) main is the entry point of the application. This is the method that JRE looks for to run the application. Therefore, in some application types (such as Swing or console) it is mandatory to have it implemented. You can see more details about this in Why is it mandatory to implement "public static void main (String [] args)"?

(String[] args)

Defines that the method should be given an array of String (named args ). In this specific case, this parameter is used in case your program needs to receive some value as argument, this is very common when the program is started by another program or by the terminal (CMD, Shell, Bash, etc.).

A very common example is Git . When you type git commit on your terminal you are calling Git with the commit parameter. All " strings " that come after the program name will be received by it within the array ( args ). Generally the first position of the array is the path that the application is in.

* Members without access modifiers are considered package-private . They will only be available within the package that are declared.

    
answered by 18.10.2015 / 02:36
source
15

public: is the visibility, which can be public , private , protected / strong>.

static: is optional, it means that the method can be called without the class being instantiated in an object, it is very useful for the main class ( main ) already which is the first one to run.

void: is the return type of the method, void is used when the method returns nothing.

main (): method name, if you have the parentheses then it is a method.

String [] args: args is the name of the local method variable and String [] . a vector of Strings.

    
answered by 18.10.2015 в 02:10