Skip to content
Related Articles

Related Articles

Understanding public static void main(String[] args) in Java
  • Difficulty Level : Easy
  • Last Updated : 25 Aug, 2020
GeeksforGeeks - Summer Carnival Banner

In Java programs, the point from where the program starts its execution or simply the entry point of Java programs is the main() method. Hence, it is one of the most important methods of Java and having proper understanding of it is very important.

Most common syntax of main() method:




class GeeksforGeeks {
    public static void main(String[] args)
    {
        System.out.println("I am a Geek");
    }
}
I am a Geek

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

  1. Public: It is an Access modifier, which specifies from where and who can access the method. Making the main() method public makes it globally available. It is made public so that JVM can invoke it from outside the class as it is not present in the current class.




    class GeeksforGeeks {
        private static void main(String[] args)
        {
            System.out.println("I am a Geek");
        }
    }
    Error: Main method not found in class, please define the main method as:
    public static void main(String[] args)
    or a JavaFX application class must extend javafx.application.Application
  2. Static: It is a keyword which is when associated with a method, makes it a class related method. The main() method is static so that JVM can invoke it without instantiating the class. This also saves the unnecessary wastage of memory which would have been used by the object declared only for calling the main() method by the JVM.




    class GeeksforGeeks {
        public void main(String[] args)
        {
            System.out.println("I am a Geek");
        }
    }
    Error: Main method is not static in class test, please define the main method as:
    public static void main(String[] args)
  3. Void: It is a keyword and used to specify that a method doesn’t return anything. As main() method doesn’t return anything, its return type is void. As soon as the main() method terminates, the java program terminates too. Hence, it doesn’t make any sense to return from main() method as JVM can’t do anything with the return value of it.




    class GeeksforGeeks {
        public static int main(String[] args)
        {
            System.out.println("I am a Geek");
            return 1;
        }
    }
    Error: Main method not found in class, please define the main method as:
    public static void main(String[] args)
    or a JavaFX application class must extend javafx.application.Application
  4. main: It is the name of Java main method. It is the identifier that the JVM looks for as the starting point of the java program. It’s not a keyword.




    class GeeksforGeeks {
        public static void myMain(String[] args)
        {
            System.out.println("I am a Geek");
        }
    }
    Error: Main method not found in class, please define the main method as:
    public static void main(String[] args)
    or a JavaFX application class must extend javafx.application.Application
  5. String[] args: It stores Java command line arguments and is an array of type java.lang.String class. Here, the name of the String array is args but it is not fixed and user can use any name in place of it.




    class GeeksforGeeks {
        // javac GeeksforGeeks.java
        // java GeeksforGeeks 1 2 3
        public static void main(String[] args)
        {
            for (String elem : args)
                System.out.println(elem);
        }
    }
    1
    2
    3

    Apart from the above mentioned signature of main, you could use public static void main(String args[]) or public static void main(String… args) to call the main function in java. The main method is called if it’s formal parameter matches that of an array of Strings.

    Can main method be int? If no, why?






    class GeeksforGeeks {
          
        public static int main(String[] args) {
      
            System.out.println("GeeksforGeeks");
        }
    }

    Java does not return int implicitly, even if we declare return type of main as int.
    We will get compile time error

    prg1.java:6: error: missing return statement
    }
    ^
    1 error




    class GeeksforGeeks {
          
        public static int main(String[] args) {
      
            System.out.println("GeeksforGeeks");
            return 0;
        }
    }

    Now, even if we do return 0 or integer explicitly ourselves, from int main.
    We get run time error.

    Error: Main method must return a value of type void in class GeeksforGeeks, please
    define the main method as:
    public static void main(String[] args)

    Explanation –
    The C and C++ programs which return int from main are processes of Operating System. The int value returned from main in C and C++ is exit code or exit status. The exit code of C or C++ program illustrates, why the program terminated. Exit code 0 means successfull termination. However, non zero exit status indicates error. Eg, exit code 1 depicts Miscellaneous errors, such as “divide by zero”

    Parent process of any child process keeps waiting for exit status of child. And after receiving the exit status of child, cleans up the child process from process table and frees the resources allocated to it. Which is why it becomes mandatory for C and C++ programs(which are prcoesses of OS) to pass its exit status from main explicitly or implicitly.

    However, The java program runs as ‘main thread’ in JVM. The Java program is not even a process of Operating System directly. There is no direct interaction between Java program and Operating System. There is no direct allocation of resources to Java program directly, or the Java program does not occupy any place in process table. Whom should it return exit status to, then. Which is why main method of Java is designed not to return int or exit status.

    But JVM is a process of a operating system, and JVM can be terminated with certain exit status. With help of java.lang.Runtime.exit(int status) or System.exit(int status)

    Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

    My Personal Notes arrow_drop_up
Recommended Articles
Page :