0

In the standard public static void main(String[] args), I have noticed that (at least in Dr. Java), you can use other words besides args, but I have never seen any code that does deviate from this name.

My questions are

  • Will using other words work in all versions of Java and development environments?
  • Is the use of args just a convention?
  • If I use other names, will it be looked down upon?

Thank you for any help! I'm having trouble finding any answers online.

Phonzi
  • 144
  • 3
  • 9

3 Answers3

3

Documentation

The docs at Oracle states, "You can name the argument anything you want, but most programmers choose "args" or "argv"."

Sachin Tiwari
  • 150
  • 13
2

Yes the args variable is just convention. The variable name can be changed, but it is convention and you would be looked down upon. Source here.

NotZack
  • 518
  • 2
  • 9
  • 22
2

args is short from arguments and purpose of that array is to hold values provided when we start Java application like

java YourClass argument0 argument1 argument2 

You can change name of any method parameter (compiler doesn't save those names anyway so they are only present at .java file, and are replaced by other values at .class file) including args, but I would leave it as. As someone said:

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

so don't do unnecessary changes.

You can also remove that argument, but this way you will create your own separate method which will be called main, but will no longer be entry point. This means you will not be able to run it directly via

java YourClass

command because JVM would be searching for public static void main(String[]) method in YourClass but since your method will not fit this signature it wouldn't be treated as valid entry point.

Pshemo
  • 122,468
  • 25
  • 185
  • 269