2

I'm not asking what is (String args[]) because that was answered here: What is "String args[]"? parameter in main method Java.

My question is why is it necessary to write it while writing main()?

I had my practical exams, where I faced a problem and realized I hadn't written String args[] while writing public static void main(). But then after writing main(String args[]) the problem was solved. (How and Why I still don't know!)

On that same day I was asked in Viva I was asked - "Is it necessary to write this String args[] while writing main()?" and thanks to the error that occurred I replied "YES" but was left without answer when asked "WHY?".
So I want to know why is it necessary to write String[] args.

9
  • Check here Commented Oct 31, 2015 at 17:45
  • The applicable keyword is Method Signature. Commented Oct 31, 2015 at 17:45
  • 2
    Note that you can also write String... args Commented Oct 31, 2015 at 17:47
  • 1
    main(String[] args) by design is entry point of Java application. This method is also used to handle potential parameters passed in console like java YourClass foo bar (foo and bar will end up in args table). Without such method Java will not know where to start. Even if you create method like main() it will not be same as main(String[] args) so you will not be able to start your application, although having such method is allowed in your class since you can have other class which can simply add code like main(String[] args){YourClass.main();} to invoke code from your mian(). Commented Oct 31, 2015 at 18:01
  • When you run a program, you can give it command line arguments. You need a way to pass these command line arguments to the program and this is a simple way of doing this. It is the Java equivalent of how the JVM is called itself. Can you explain your doubt? Commented Oct 31, 2015 at 18:21

3 Answers 3

2

From Java Language Specification 12.1.4

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String. Therefore, either of the following declarations is acceptable:

public static void main(String[] args)

public static void main(String... args)

(note that you can't have two main methods with String[] and String... in same class, since varargs are simply syntactic sugar which at compilation time will be replaced with arrays so you would end up with two methods handing String[] and one class can't have two methods with same name and parameters)

So when you execute command like

java YourClass foo bar 

Java Virtual Machine will place foo and bar parameters in String[] array and will try to pass that array to main method which can accept it as parameter.

This method is also used when command doesn't have any arguments like

java YourType 

This decision simplifies our life because we don't need to focus on handling cases where there are two entry points

  • one for command with arguments
  • and one where command doesn't have any arguments.

We can simply allow user to pass arguments but if we don't wan to handle them we can simply ignore them.

Also remember that we are allowed to have in our class any method which has proper declaration (and doesn't violate any rules inherited from superclass like widening member visibility - we can't make protected method public), so there is nothing wrong with having

public static void main(){ /*your code*/ } 

But you need to realize that this method can't be used as entry point, so if you want to start your application from this method you will need to create proper main method which will execute your main() method:

public static void main(String ...){ main(); } 
Sign up to request clarification or add additional context in comments.

Comments

2

In Java the entry point has to be a public static void main(String[]) method. This is simply what is called when you run your class. A main() method with no arguments will not be called. If you have no arguments in your command you are merely calling main(String[]) with an array of length 0.

To address the Viva question, the parameter's identifier is used by the compiler only and can be changed, along with the alternate placement of the [] or using String... to specify an array. So it appears to be a technicality, but you can use any of the following:

  1. public static void main(String[] args)
  2. public static void main(String args[])
  3. public static void main(String... args)
  4. public static void main(String[] custom_identifier)
  5. public static void main(String custom_identifier[])
  6. public static void main(String... custom_identifier)

1 Comment

Thanks for pointing out all the possible ways to write a legit 'main()' declarations! Helped a lot!
1

It's for the command arguments. Without them, the JVM would have to perform a check to see if the method contains arguments before attempting to call the method, determining whether it could do main() or main(args) (two syntactically correct methods with different ways to call them)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.