Understanding “static” in “public static void main” in Java

Following points explain what is “static” in the main() method:

  1. main() method: The main() method, in Java, is the entry point for the JVM(Java Virtual Machine) into the java program. JVM launches the java program by invoking the main() method.

  2. Static is a keyword. The role of adding static before any entity is to make that entity a class entity. It means that adding static before methods and variables make them class methods and class variables respectively, instead of instance methods and instance variables.
    Hence, static methods and variables can be directly accessed with the help of Class, which means that there is no need to create objects in order to access static methods or variables.

    // Making a function as static
    static void func()
    // Making a variable as static
    static int var;
  3. Static methods: When a method is declared with static keyword, it is known as static method. As discussed above, any static member can be accessed before any objects of its class are created, and without reference to any object.
    // Making a static function
    class GfG
        static void func()
    // Calling a static function
  4. Static main() method: When the static keyword is added in the function definition of main() method, then it is known as static main() method.
    class GfG
        // Making a static main function
        public static void main(String[] args)
  5. Need of static in main() method: Since main() method is the entry point of any Java application, hence making the main() method as static is mandatory due to following reasons:
    • The static main() method makes it very clear for the JVM to call it for launching the Java Application. Otherwise, it would be required to specify the entry function for each Java application build, for the JVM to launch the application.
    • The method is static because otherwise there would be ambiguity which constructor should be called.

      Example, if the class looks like this:

      public class GfG{
        protected GfG(int g){}
        public void main(String[] args){

      The JVM now enters an ambiguity state deciding whether it should call new GfG(int)? If yes, then what should it pass for g? If not, then should the JVM instantiate GfG without executing any constructor method?

      There are just too many edge cases and ambiguities like this for it to make sense for the JVM to have to instantiate a class before the entry point is called. That’s why main is static.

    • The main() method is static because its convenient for the JDK. Consider a scenario where it’s not mandatory to make main() method static. Then in this case, that just makes it harder on various IDEs to auto-detect the ‘launchable’ classes in a project. Hence making it a convention to make the entry method ‘main()’ as ‘public static void main(String[] args)’ is convenient.

    Thus, the main() method in java is declared as static so that the JVM can access it directly using its class name before object creation.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.