public static void main(String args[])


public static void main is discussed in Simple terms for a very Beginner.

In Java, the execution starts from main() method. But for compilation, main() is not required. Java's main() method syntax is quiet different from C/C++.

Following is the public static void main complete signature

public static void main(String args[])

Every word in the public static void main statement has got a meaning to the JVM.

1. public: It is a keyword and denotes that any other class (JVM) can call the main() method without any restrictions.

2. static: It is a keyword and denotes that any other class (JVM) can call the main() method without the help of an object. More about static is available at static Keyword – Philosophy.

3. void: It is a keyword and denotes that the main() method does not return a value.

4. main(): It is the name of the method.

5. String args[]: The parameter is a String array by name args. The string array is used to access command-line arguments.

The Java program under execution takes some context area (execution area) in the RAM. JVM is in another context area. To call the main(), the JVM requires an object. When the JVM has not entered into the program, how it can create an object or get an object. To overcome this, allow the JVM to access the main() without object just by declaring static. Once the JVM enters, it can create hundreds of objects later.

Whether you pass right now command-line arguments or not, you must have the string array as parameter as it is the part of syntax.

Any word is missed in the above statement, the program compiles, but does not execute.
A program using main() is discussed in "Basic Class Structure, Compilation and Execution".

1. public static void main(String args[]) throws IOException: This sort of main() method is used in "BufferedInputStream and BufferedOutputStream" and in many places.

2. public static void main(String args[]) throws Exception: This type of main() method is used in "Communication with TCP/IP Protocol" and also in many places.

Other interested topics, you definitely like.

1. Java Naming Conventions – Readability
2. System.out.println
3. Java Drawbacks
4. Strongly typed language
5. Data Types Default Values – No Garbage

24 thoughts on “public static void main(String args[])”

  1. vishalkumar mehta

    hi I AM VISHAL KUMAR…I WANT TO ask a question..???
    why string arguement is used in public static void main..???

  2. Respected Sir,

    I m aashish & i am gujarati from Gujarat
    I read so many books & learnt from other senior teachers also, but still I can’t understand the meaning of
    “public static void main()”. pl. can you give me explain about in Hindi or Gujarati Lang.

    Thanks

    1. Hi Aashish Shah ,here’s your answer
      public static void main(String args[])
      PUBLIC:we Used public access specifier here because public access specifier helps the JVM(Java virtual machine) to easily access the main method.
      STATIC:Static are used here because of main() function is the first function which called without using of Class Object(So, to remove this thing static helps to say JVM at the runtime to use the CLASSNAME.MAIN() to call the main function)(Note: May u know about of it in JAVA language we can’t call any method without class Object. So “static” helps to call function using of Class name at the time of Run time.)
      VOID: void is a return type (but it returns nothing) its depend on you what type of data you want from your function. we can also use int,float,double datatype by replacing VOID.
      STRING ARGS[] : Its used for command line argument.(Take input at runtime)
      May be these explanation helps you to understand about of public static void main(String args[])

  3. Seriously you have written too good content in a such an easy language. The information is so useful and easy to understand.
    Can you write something on like how the compiler and the bytecode of Java works?
    LIke from the starting
    1) u write a java program
    2) then javac ………….
    3) what exactly happens next at system level

    hope if you could write i really wanted to know ….

  4. Sir, i have one question not related to above thread.
    Can we insert duplicate keys in a hashmap ?
    We can insert it but by doing so, previous data gets overrided.
    example –
    Map m = new HashMap();
    m.put(“a”,”apple_one”);
    m.put(“a”,”apple_two”);
    m.put(“a”,”apple_three”);
    System.out.println(m.entrySet());

    It will print only [a=apple_three]
    But i want to print like below

    [a=apple_one,a=apple_two,a=apple_three]

    Is this possible ? how ?

      1. If you write int main (and you got predefined void main also in the code), it is not allowed as it does not come under method overloading. If you write with your own main returning int and do not contain predefined, it becomes your own user defined method. JVM will not call your mehthod.

Leave a Comment

Your email address will not be published.