Wednesday, December 5, 2018
Home > java > Java Main Method

Java Main Method

Java main function / method is the commonly used and the most important method in the language. Whether you are writing a single class hello world program or a very large application. You must have a class with a java main method as it defines the entry point of the java program or application.  A main method in java can be defined using the following syntax:

public static void main (String [] args){

// body of java main function.

}

A main function is also available in other languages like c++, c# etc.

In c++ we have a main function as:

int main() {

// body of the function

}

A c# main function in a class as:

static void Main(string[] args){

// body of the function

}

public static void main (String [] args)

Now we can have a look at different parts of the signature of the function.

public:

public is the access specifier for the main function. It is necessary to keep the main method public as it defines the entry point so if we don’t keep it public then no program can execute it.

public class abc {
    static void main(String[] args){
         System.out.println("class with non-public function");
     }
}

If we try to keep the function non-public then we will get the following error on console.

java main method error non-publicstatic:

Main method should be kept static so that the jvm can load the class into memory. The reason behind it is that as it is the entry point and when the java runtime starts we don’t have any class object present. A non- static main function will give us the following error in the console.

public class abc {
     public void main(String[] args){
            System.out.println("non-static main function");
     }
}

void:

As we know that every function must have a return type. And void is the return type of main function in java as the main method in java doesn’t returns any value. A question arises here what if we change the return type of the main function to any other data type? The answer to this question is that we will get an error like below:

package example;
public class abc {
     public static int main(String args[]) {
          System.out.println("changed return type of main function");
          return 0;
     }
}

java main method error int

main:

It is the reserved word that defines the name of the function.

String [] args:

Java main method accepts the command line arguments. These arguments are of string type. We can define the command line arguments as: String args []

public class abc {
     public static void main(String[] args){
          for(String s : args){
               System.out.println(s);
          }
    }
}

java main method command line args

Another signature for main method in java is:

public static void main(String…args) { }

Java main method must reside in the class. And it can be any class in the application. We can have more than one main methods in our application.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.