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.

Community
  • 1
  • 1
abhishah901
  • 539
  • 1
  • 8
  • 16
  • Check [here](http://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-java) – sam Oct 31 '15 at 17:45
  • The applicable keyword is Method Signature. – Tassos Bassoukos Oct 31 '15 at 17:45
  • 2
    Note that you can also write `String... args` – Stephan Bijzitter Oct 31 '15 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()`. – Pshemo Oct 31 '15 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? – Peter Lawrey Oct 31 '15 at 18:21
  • Don't you guys understand English? That question does not signify its importance! Those answers tell me what it is! Read the question multiple times before flagging it duplicate. Don't go around flagging duplicates just because you can! None of those answers answer my question! And before flagging a question duplicate try asking the person who asked the question if the question you people claim its duplicate of answers the questioners' question or not!@BackSlash – abhishah901 Nov 01 '15 at 08:00
  • Don't you guys understand English? That question does not signify its importance! Those answers tell me what it is! Read the question multiple times before flagging it duplicate. Don't go around flagging duplicates just because you can! None of those answers answer my question! And before flagging a question duplicate try asking the person who asked the question if the question you people claim its duplicate of answers the questioners' question or not!@DaveNewton – abhishah901 Nov 01 '15 at 08:01
  • Thank you @Pshemo! Its a reasonable reason! Try writing down that answer so I can accept it as a Legit one! Thank you! Btw a link reference would be good too! Thanks anyway! – abhishah901 Nov 01 '15 at 08:03
  • What if I don't wanna pass any arguments! Why is it mandatory even when not required??? @PeterLawrey – abhishah901 Nov 01 '15 at 08:13

3 Answers3

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();
}
Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
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)
Linus
  • 894
  • 7
  • 13
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)

Vince
  • 14,470
  • 7
  • 39
  • 84