Back to Top

Sunday, 15 January 2012

JAVA : public static void main(String args[]) - Explanation.

public - public means everyone can access it.That means it's in global scope. As, main method is called by JVM [Java Virtual Machine], which is actually out of the project scope, the access specifier of this method is public.


static - Java environment should be able to call this method without creating an instance of the class , so this    method must be declared as static.

void - the return type of this method is void means there's no return value.



main( ) - the name of the method, main because it's the main method.

String args[] - arguments to this method. This method must be given an array of Strings, and the array will be called 'args'.



RELATED POST ~ JAVA INTERVIEW QUESTIONS

Anijit Sarkar

28 Responses to “ JAVA : public static void main(String args[]) - Explanation. ”

Mubarok said...
14 April 2012 13:00

thanx


anij said...
15 April 2012 08:49

@Mubarok
you r welcome my friend!!
thanks for commenting!! :)


Java ArrayList tutorials said...
18 April 2012 15:10

Good explanation. Why main is made public static in Java is also a popular question where this explanation can be used.


Bharath said...
15 July 2012 22:42

excellent explanation about usage of static in main method......


anij said...
15 July 2012 23:15

@Bharath....
...Thanks a lot my friend!!


Anonymous said...
19 July 2012 18:54

THNX..


Anonymous said...
27 July 2012 11:59

shukriya boss...really worthy explanation


Kritika Misra said...
1 September 2012 13:50

it was helpful


cse siva said...
5 September 2012 14:57

i not satisfy with the main() explanation , pls give clearly


prasad said...
14 September 2012 06:17

thanks ,it is really helpful............


sudhanshu said...
15 September 2012 00:17

We can also use
public static void main(String ...args) instead of

public static void main(String args[])



Anonymous said...
3 October 2012 13:41

yeas Sudhanshu (String args[]) is as same as (String...args)


Gobirajan Arumugam said...
11 October 2012 12:22

thanks friend,very useful info..
public static void main(String []args) this is also one of the method to declare string array.


zaid said...
12 October 2012 18:49

Than,s FRND


Noor Ali Butt Broz. said...
30 October 2012 22:55

what if declare main() as private or protected ?????


Anonymous said...
5 December 2012 08:44

super explain


saitheja reddy said...
16 December 2012 22:16

can we define main as private or protected?


Ramesh Kumar said...
19 February 2013 20:05

main() method tell us the begin of the program execution


arun kumar said...
17 March 2013 17:18

we can define main() as private or protected and ofcourse The program compiles properly but at runtime it will give “Main method not public.” message.


Anonymous said...
23 March 2013 10:23

why should "s" be capitals in "String args[]" ?


Anonymous said...
6 April 2013 21:41

instead of String can we use datatypes like int,char....???


Anonymous said...
23 April 2013 12:39

's' in "String agrs[]" is capital beacause String is the name of class we are using and it should be capital


Sunkara Krishna Teja said...
18 July 2013 10:33

What do you mean by public-every one can access it


Anonymous said...
9 August 2013 09:08

can i use private instead of using public


anij said...
9 August 2013 23:16

No!
Main is call by JVM [Java Virtual Machine],
which is actually outside the project,
so, the access specifier of this method must be public.


Anonymous said...
8 September 2013 19:36

Main - JVM has to know the name of the method it is going to run. It could have been called "lollipop"for all the JVM cares as long as it is known and constant.

String[] - running a program from the command line means you can pass arguments to it. This is where those arguments get placed so that your program can handle them.



Anonymous said...
8 September 2013 23:10

Hi, Have one question..
Why the parameter of String[] needs to be passed in the main method, do we use it by somehow?
Please reply.. waiting to hear you soon..


anij said...
8 September 2013 23:40

The parameter of String[] is for for command-line arguments in Java.
Check this link for command-line arguments : http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html

Or, in Eclipse also, you can simply try this:

Code:
public class TestMain {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.err.println(args[i]);
}
}
}

and now -> Right click -> run as -> run configurations -> select this class -> go to Arguments tab -> write some thing on Program arguments text area -> click apply -> clcik run

your inputs will be printed on eclipse console


Post a Comment

Popular Posts

Subscribe via Email
Subscribe Java Interview Questions via Email
All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar
blog links Web Directory Academics Blogs