Main Method in Java

Introduction

The main method is the first method in the Java program. It is the entry point of core Java programs. It is required that a program should have at least one main method.

The main ()

In core Java programs it is known as the entry point of the program. It is also the first method in any core Java program. Execution of the program starts at the main method in core Java programs. If we do not have a main method in our program then the JVM will give an error. The main method in Java is very much similar to the main function of C and C++. In C and C++, first the main function is called. Similarly in Java the first main method is called. Execution of the program starts with the main method and stops when the method finishes completely.

Signature of main()

The signature of main() is:

public static void main(String[] args)

It is the signature of the main method that is to be followed by all the core Java programs. Following the introduction of varargs, the following is also the signature of the main method:

public static void main(String... args)

But this signature will only work in Java 5 and above versions of Java.

Some important points related to main ()

  • The main method is public because it should be visible to all the other classes, whether of the same package or of a different package. If it is not public then it is not available to all the classes in Java.

    main() without public.jpg
     
  • The main method is static because the Java Virtual Machine can call it without making its object. Because there is not any rule in Java to instantiate the main method or we can say that the JVM does not know how to instantiate the main method in Java.

    main() without static.jpg
     
  • If we declare the main method as private then it will show an error.

    main() with private.jpg
     
  • If the String[] argument is not given to the main method then it will also show an error.

    main() without String[].jpg
     
  • This String type array is empty.
     

    package demo;

    publicclassDemo

    {

        publicstaticvoid main(String[] args)

        {

            System.out.println(args.length);

        }

    }


    empty array.jpg

     

  • In Java program multiple classes can have a main method, but a single class cannot have multiple main methods.

    multiple main() in one class.jpg
     

  • We cannot override the main method because it is a static method and in Java static methods cannot be overriden.
     

  • The main method is void because in Java the main method will not return a value.
     
  • The main method in Java is similar to the main function of C and C++ in the way that it is called first in the program whether it is Java or C or C++. But the main method in Java is different from the main function of C and C++ in the way that in Java the main method will not return a value, but in C and C++ the main function will return an int value.

Summary

This article has explained the main method and the importance of the main method in Java that is very important for a programmer.

Up Next
    Ebook Download
    View all
    Learn
    View all