Inicial > Programação, Tecnologia > “public static void main(String args[])” is not alone!

“public static void main(String args[])” is not alone!

For those who are starting to learn the Java programming language, it is very common to see people say things like “Your main function must always look like this:”


“Otherwise, it won’t compile!”

If you buy this statement you will fall for it when you go take the SCJP certification. There are many other ways in which the main method can be declared. Here are some of them:

To simplify, you can follow these rules:

  • The only access modifier allowed is public and it must be present.
  • The static keyword must always be present as well.
  • The args argument does not need to be called args. It can have any name you want (as long as that name follow the rules of a valid Java identifier, of course)
  • Once you put the method’s return type, you need to put the name of the function right on way. This is valid in any method declaration, not only main.
  • The main method must always return void. It can’t return anything else. This is specially trick for C/C++ programmers, once in those 2 languages you could write something like int main().

That’s it folks! If you have any questions or improvements to suggest, feel free to put them on the comments!!

PS: If you saw any mistake in my English, please, send me an email or a comment telling me. I’m Brazilian, therefore I’m not an English native speaker. I would really appreciate being corrected! Thank you in advance!

Anúncios
  1. Nenhum comentário ainda.
  1. No trackbacks yet.

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair /  Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair /  Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )

Conectando a %s

%d blogueiros gostam disto: