Explain public static void main (String args[])…..

Explain public static void main (String args[])…..

The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member can be accessed by code outside the class in which it is declared.In this case, main( ) must be declared as public, since it must be called by code outside of its class when the program is started. The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made. The keyword void simply tells the compiler that main( ) does not return a value. As you will see, methods may also return values.
As stated, main( ) is the method called when a Java application begins. Keep in mind that Java is case-sensitive. Thus, Main is different from main. It is important to understand that the Java compiler will compile classes that do not contain a main( ) method. But the Java interpreter has no way to run these classes. So, if you had typed Main instead of main, the compiler would still compile your program. However, the Java interpreter would report an error because it would be unable to find the main( ) method.
Any information that you need to pass to a method is received by variables specified within the set of parentheses that follow the name of the method. These variables are called parameters. If there are no parameters required for a given method, you still need to include the empty parentheses. In main( ), there is only one parameter, albeit a complicated one. String args[ ] declares a parameter named args, which is an array of instances of the class String. Objects of type String store character strings. In this case, args receives any command-line arguments present when the program is executed.

68 responses to “Explain public static void main (String args[])…..

    • The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made.

    • public static void main (String args[]) { ….// This only get called … }

      If this method with this signature missing you will get:
      Exception in thread “main” java.lang.NoSuchMethodError: main

  1. hi, i’m a new learner of java. My problem is to remember java language meaning e.g. public static void main etc. i can remember the word but not it’s meaning. So its really a matter? or i’ll get it later?

    • We cant speak any language if we don’t know meaning of words…doesn’t matter how fluently you pronounce it.

      So knowing meaning of key”words” matter most to learn any programming language, and there are hardly 50 keywords in Java to learn about.

      PS: “main” is not a keyword.

      Good luck!

  2. Why are “static” and “String” needed in “public static void main”? can you explain please.. I got an assignment about this.
    Thankyou :)

    • The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made.

  3. hiiiiiiii…can u tell me why jvm always srch in main method of String as a argument type……………..??why not others

    • Array in main method String args[] used to hold command line arguments which are need to be passed before execution starts. This can be anything say file name or any number . So if it was integer array int args[] cant hold file name…. but number can be stored as String and later cast it into number format.

    • The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made.

  4. Sorry for posting multiple comment. can u please explain (1)
    what does “final” keyword mean in my main method? i.e. public static void main(final String… args).
    (2) why “[ ]” is removed after “final” keyword?

    (3)what does ” final Set something = new HashSet();” mean? (specially why set and HashSet is used for?)

    • It wont give you any compile time error as main(int[] arg) is valid method. Though you will get no main found… run-time error when no p s v m main(String[] args) method in class.

    • It wont give you any compile time error as main without arguments is valid method. Though you will get no main found… run-time error when no p s v m main(String[] args) method exist in class.

    • public static void main(String[] args){}
      public static void main(String[] aa){}
      public static void main(String aa[]){}
      public static void main(String… aa){}

      All of above will work….

      I am not sure what you are asking about small s in string will surely give you compile time error.

      or if you are asking about argument name viz args ,aa etc, you can use any valid name.

  5. hey……..i hav a question that whats the need of String[] args in main ?i can define it inside the main function so why is it that necessary that even without this it gives a compile time error……

    • Main is not a magical or extra-ordinary method. It works like any other method.

      String[] args is an input to main method so it does not make sense , to declare it inside method.
      That is basic syntax to declare argument or parameter inside parentheses.
      Additionally String[] args are used to store command line arguments.

      Also main without parameter must not give any compile-time error but runtime.

  6. u mean to say that the syntax of psvm is pre-defined in JVM like this only that why it shows error at runtime……….if yes then the main function without argument act as a static member function of the class so in that class then why it shows run time error…..

    • main is just a method .Any Method’s syntax in java is predefined.

      SO PSVM () is valid syntax for any method
      But what JVM look for is psvm(String args[]) and since same is not present gives run-time error.

  7. String args[] accepts command line arguments. but if i do not want to pass any command line arguments then ?
    why i cannot use only String args.

    • Ok lets think in other way…

      JVM is a program that designed to run other programs.

      SO JVM need a start point to run any program, but how would JVM knows which method to run first.

      Thus we define psvm main (String args[]) in our application in order to let JVM know this is start point of application.

      And JVM always require this standard method in our application as a starting point.

      Now answer to your question :
      It doesn’t matter whether you pass command line argument or not ….. this is Standard generic method which required for JVM.

      Hope it helps!

  8. 1)If we use int instead of static n return some value will it run or not ?

    2) if we interchange the static n void with each other will show any error?

    3) if we not write static in main will code run not? If not, whether its compile tym error or run time error?

    • If you are calling any method which takes parameter then you have to pass required parameters.

      In case of main , JVM is responsible for calling main method, so JVM will pass parameters at the time of calling main method defined by you.

      If you explicitly calling main method in that case you have to pass Sting array.No matter if it is empty.

  9. Actually java interpreter will call the main method , so what arguments it will pass when calling main method….

  10. What is a command – line ( that are passed as argument to the main() by JVM… as you stated earlier…if I’m not wrong) ??
    would you please give an example of command-line ???

    • Command line arguments are input that we pass to application.

      For example you want to send email to list of email ids.

      So you have two files

      1. emailList.txt
      2. message.txt

      So your code goes something like


      public class EmailSender{

      public static void main(String args[]){

      String emailIdFileName=args[0];
      String messageFileName=args[1];

      //some method that reads file and return list of email ids
      List emailIdList = readEmailList(emailIdFileName);

      //Some method reads mesage from message file and returns its content.
      String message=readMessage(messageFileName);

      //some method sends email
      emailSender(emailIdList ,message);

      }

      }

      Now if you can see we have used args[0] and args[1] so we have to pass these values

      Compile your code and run it from command prompt (windows environment) as:

      />java EmailSender emailList.txt message.txt

      we are passing name of both files as a command line argument

      These arguments are passed as String array to main method.

      Hope this helps

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s