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

Asked

Viewed 48,974 times

35

I want to understand what each item of public static void main(String[] args) in Java and when to use.

2 answers

45


public

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

Other modifiers are protected, private or unmodified1. Here can be read more about Java access modifiers.

static

Defines the method as static, this means that the class does not need to be instantiated to call this method.

In the example, I have the class Cliente with the methods (static) FazerAlgo() and (non-static) FazerAlgoDois(), the use 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 the return type of the method. This return type means empty/nothing, the method gives no return. Methods can return any type of your project, even those created by you.

main

It is the name of the method. Every method needs to have a name. The names are defined by the programmer and usually follow some conventional pattern previously defined by the language or by the community, although this is optional. In the case of Java, Oracle itself defines these conventions. The convention on the appointment of methods says:

Methods should be Verbs, in Mixed case with the first Letter lowercase, with the first Letter of each Internal word capitalized.

In free translation:

Methods should be verbs, in "Mixed case" with the first lowercase letter and the first letter of the inner words in uppercase letter.

In Java (and other languages as well) main is the application entry point. This is the method that the 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 it is mandatory to implement "public Static void main (String [] args)"?

(String[] args)

Defines that the method should receive an array of String (nominee args). In this specific case: this parameter is used in case your program needs to receive some value as an 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 the Git. When you type git commit on your terminal is calling Git with the parameter commit. All the "strings" that come after the name of the program will be received by the same within the array (args). Usually the first position of the array is the path that the application is.


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

18

public: is the visibility, which may be public, private, protected or default.

Static: is optional, means that the method can be called without the class being instantiated into an object, is very useful for the main class (main) since it is the first to be executed.

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

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

String[] args: args is the name of the local variable of the method and String[] the data type, this type is a string vector.

  • 7

    Correction: este tipo é um vetor de caracteres.. String is a vector (or array) of characters, String[] is an array of String.

  • Thank you @jbueno

  • The static is optional?

  • @Renan. For the médotos in general, yes, but for the main class it is necessary.

Browser other questions tagged

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