public static void main(string args[]) Explanation

class JBT{
public static void main(String args[])
{
System.out.println("Hello JBT");
}
}

In the above application example we are using public static void main. Each word has a different meaning and purpose.

Public : is an Access Modifier, which defines who can access this Method. Public means that this Method will be accessible by any Class(If other Classes are able to access this Class.).

Static : is a keyword which identifies the class related thing. This means the given Method or variable is not instance related but Class related. It can be accessed without creating the instance of a Class.

Void : is used to define the Return Type of the Method. It defines what the method can return. Void means the Method will not return any value.

main: is the name of the Method. This Method name is searched by JVM as a starting point for an application with a particular signature only.

String args[] : is the parameter to the main Method.

Series Navigation<< Java Reference VariableWrite Hello World program using Eclipse >>
By | 2012-10-12T21:14:25+00:00 October 12th, 2012|Core Java|51 Comments

About the Author:

51 Comments

  1. Sanjeevkumar November 4, 2012 at 4:50 pm - Reply

    Hi,
    Good Explanation…
    Thanking you….

  2. Gaurang July 18, 2014 at 1:10 pm - Reply

    Please fix your grammatical errors. It makes understanding this content difficult.

    • a October 10, 2016 at 6:00 pm - Reply

      content is gud…dont alwayz look at grammar

    • sf November 5, 2016 at 5:16 pm - Reply

      u go for a tuition to learn grammar and become an English professor. you are learning java or checking english errors!

      • xyz January 9, 2017 at 9:03 am - Reply

        right ,we have developer ,most engineering pass out in first but those student not able to do coding this is different between software developer and english gentleman.

        • xyz January 9, 2017 at 9:04 am - Reply

          this is very nice info,thank you so much for author.

  3. Akash Dhande September 16, 2014 at 2:08 pm - Reply

    Hello Sir ,
    Nice Info wanted to ask what are the parameters does main method takes
    thanku

    • Abhishek December 27, 2015 at 5:42 pm - Reply

      String args[]

  4. Jay February 4, 2015 at 3:26 pm - Reply

    String args[] : Parameter to main method…

    Please site how to use the main() with values given to parameters (like what the difference would be with (or without) these values).

    • naresh May 16, 2015 at 5:47 am - Reply

      If u dont use it shows error in execution….that y we r passing infinite arguments in String type…string is one.type of class ….so,first letter is capital

  5. Shubham Choudhary June 8, 2015 at 3:27 am - Reply

    hi!!!
    Nice Explanation
    Thanx

  6. Priyokumar August 20, 2015 at 12:12 pm - Reply

    Why “String[] ags” is passed as parameter of main function and without passing this parameter what will happen?

    • J Singh August 22, 2015 at 7:11 pm - Reply

      HI,

      This is a standard which needs to follow if we want jvm to search it to execute it. There is no harm in removing this parameter from method but then that method will be treated as other java methods and it will not be directly called by JVM.

    • Abinaya October 20, 2015 at 5:48 am - Reply

      you will get a compilation error if you dont use that

    • Ankita January 7, 2017 at 3:19 pm - Reply

      The main method will compile successfully but will not run. As it will be treated as any other method.
      Certain standard to be followed for main method

  7. Pravin October 4, 2015 at 6:53 pm - Reply

    It’s really nice & understandable words.

  8. abhinay October 7, 2015 at 12:23 pm - Reply

    Good Article
    thanks

  9. Achyuth November 12, 2015 at 11:15 am - Reply

    i want more explanation. i know this information.

    • sf November 5, 2016 at 5:19 pm - Reply

      read java book. java complete reference

  10. Saydur November 27, 2015 at 5:26 pm - Reply

    Thank you so much! that’s a good article. I appreciated you

  11. joel December 5, 2015 at 9:33 am - Reply

    great article.thnk uu

  12. suresh February 1, 2016 at 6:42 pm - Reply

    Very Good Explanation Thank you

  13. Divya February 7, 2016 at 6:57 am - Reply

    My program still runs if I use other word instead of args in “String[] args”(Parameter to main method). I want to know the reason behind it.

    • J Singh February 10, 2016 at 4:06 am - Reply

      Hi Divya,

      args is just a name of argument. You can use any name you want but type should be array of string,
      Hope it clarifies.

      Regards

  14. yogi March 1, 2016 at 4:17 am - Reply

    good explanation but give me parameters at the same time explanation. i mean which type of parameters in using java.
    and please give me different meanings Hello and hello in java

  15. Sachin March 11, 2016 at 8:38 am - Reply

    Hi,

    I want to know why JVM treats main(String [] args) method as a starting point of program, can anyone tell me the internal functioning of this method, like how it gets execute and what threads are required to execute this method so that JVM can understand this method as the starting point of the program.?

    • Shashi November 9, 2016 at 4:21 pm - Reply

      Suppose you are watching a movie and the scene of movie starts from middle of a story and as you continue watching movie, u need to understand the current scene of movie and for that movie makes gives u a starting part of movie in somewhere middle of movie(Gives you information something like” 15years before ” in movie)..by reading this words in movie you get to know the starting of movie…
      So likewise u place you main method anywhere in program(inside class) the JVM will read full code in its language to find main() method and starts acknowledging(executing) the code from main method…
      and if you dont provide main() method in code the JVM will not understand the story of your code will give error…

  16. Mother May 19, 2016 at 5:15 pm - Reply

    The grammar in this article and in the comments is horrendous.

    • sf November 5, 2016 at 5:20 pm - Reply

      u write a new english dictionary

    • Ghengis May 4, 2017 at 3:38 am - Reply

      It is shortcut English grammar that is enough for developers to understand each other so they can write efficient English with minimal words – like good programming style that is less verbose

  17. Vinod June 29, 2016 at 7:24 am - Reply

    hi can you explain why public static void should write in the same order/sequence as written now.

    • J Singh July 6, 2016 at 11:23 am - Reply

      Hi Vinod,

      Public and static keyword position can be changed. There is no such rule of using public before static. But as void is method return type it should come just before method name which is main in this case. Hence this sequence.
      public static void main
      static public void main.

      Both are same. But as a standard we right as in the first case.

  18. fekk tom July 7, 2016 at 12:38 pm - Reply

    Thanks
    I guess this will useful for me

  19. sapna suryavanshi July 13, 2016 at 6:52 pm - Reply

    Why we usethis method main(string args())

    • J Singh July 14, 2016 at 6:12 pm - Reply

      It works as starting point of application execution.

  20. sapna suryavanshi July 13, 2016 at 6:55 pm - Reply

    I want list and description of in build classes and their sub classes…like printsteam is class and println() is its method…

  21. Shrikant July 21, 2016 at 5:33 am - Reply

    why we pass only string as the parameter of the main method?

    • J Singh July 22, 2016 at 10:45 am - Reply

      Argument passed in main method is array of string and not string. This is the standard followed.

  22. Mahesh shinde August 29, 2016 at 1:53 pm - Reply

    what is array of string

    • J Singh September 3, 2016 at 10:00 am - Reply

      It is a argument passed to Main method.

      Regards

  23. maria October 8, 2016 at 2:18 pm - Reply

    what is command line argument

    • Shashi November 9, 2016 at 4:27 pm - Reply

      Command passed at time of runtime..
      On console you pass your input as an argument..

  24. Ashwini October 12, 2016 at 5:32 pm - Reply

    Can you please explain…how internally works these SYSOUT statement??
    System.out.println(“args[” +i+ “]:”+args[i] );
    System.out.println(“args[ i ]:”+args[i] );

  25. venkat January 6, 2017 at 10:15 am - Reply

    thank u for update ur information
    i have some cofusion in null point exception
    please send me a link of ur site in null point exception

    • J Singh February 8, 2017 at 7:03 pm - Reply

      I dont have any article specifically written on null pointer exception

  26. yaksh July 15, 2017 at 11:40 am - Reply

    hello sir if we not use static in with voidmain() how we run void main() function
    without static keyword

    • Vivekanand July 16, 2017 at 6:16 pm - Reply

      You can write main method without static keyword, there will not be any issue. But it will not work as expected. It will be treated as normal method and in order to execute it you need to call given method explicitly. While static keyword will make this method to be treated as entry point. Hope it clarifies.

  27. hema August 20, 2017 at 6:51 am - Reply

    why we are using string[] args in pubic static void main(String[] args) why we are not using character array or int array i need elabrate answer

  28. Priya August 29, 2017 at 5:53 am - Reply

    Main method is very important in java. Program execution starts from main () method only.

  29. vicky September 15, 2017 at 7:17 am - Reply

    thank u for the given content\

Leave A Comment