Public static void main(String[] args)
please explain why here we make parameter array and why it is static what is args [].
-
static because the JVM shouldn't need to construct any objects before starting the application and `String[]` because you may invoke the application with string arguments from command line. – aioobe Mar 11 '15 at 07:39
-
thank you for your answer here a little bit confusion i.e what is jvm and what is it work in java. – rahil Mar 11 '15 at 07:50
-
See [Lesson: A Closer Look at the "Hello World!" Application](http://docs.oracle.com/javase/tutorial/getStarted/application/) and [Command-Line Arguments](http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html). – Jesper Mar 11 '15 at 08:31
3 Answers
static >It is used with main() and provide the advantage of static method that means there is no need to create an object to invoke the main method.
void > The main() is declared as void because it doesn't return any value.
String[] args >This parameter is used for command line arguments.so whatever argumnets passed on command line will be collected in args[] array.
for example
class CommandLineExample{
public static void main(String args[]){
System.out.println("Your first argument is: "+args[0]);
System.out.println("Your second argument is: "+args[1]);
}
}
compile by > javac CommandLineExample.java
run by > java CommandLineExample India USA
output:
Your first argument is: India
Your second argument is: USA

- 1,741
- 14
- 21
-
thank you for your answer here a little bit confusion i.e what is jvm and what is it work in java. – rahil Mar 11 '15 at 07:50
When you launch your application from command prompt, then java interpreter looks for public static void main(String[] args)
method in your application class and invokes it.
All command line parameters are passed to this method as array of Strings.
For instance if you call:
java MyClass param1 param2 param3
java interpreter invokes public static void main(String[] args)
of MyClass
class and passes an array with elements param1
, param2
, param3
as args
parameter for this method.
If your class MyClass
does not contain public static void main(String[] args)
method, then you will get an error.
And let's explain public static void main(String[] args)
modifiers.
public
- to be accessible outside of your class package.static
- because it does not connected toMyClass
instance, it can be invoked without building an object ofMyClass
.void
- because this method returns nothing.
public
means that main() can be called from anywhere.
static
means that main() doesn't belong to a specific object.
void
means that main() returns no value.
main
is the name of a function. main() is special because it is the start of the program.
String[]
means an array of String.
args
is the name of the String[] (within the body of main()). args
is not special; you could name it anything else and the program would work the same.

- 1
- 2