Java main method explained

17th December 2018 9 By Alin Bistrian

What is the Java main method? Why do we need to have one in our java program? Does it always have to be precisely public static void main(String[] args) {}? If yes, WHY?

 

Let us try answering these questions assuming that you have a basic understanding of Java Development Kit and Java modifiers.

 

What is the Java main method?

The main method is the entry point of our Java Program. It is where the JVM (Java Virtual Machine) start executing our program. It has to be declared in a Java class.

 

A class that has the main method is called Main Class, and even though it does not have to be named MainClass, it can have any other name.

 

Why do we need to have one in our java program?

It is more than obvious why we need one, and that is because we need to tell JVM where to start executing our program. The main method is also called program/module entry point.

 

Does it always have to be precisely public static void main(String[] args) {}?

The answer is yes, except parameter declaration which can vary. I will explain these declarations a little later.

 

public – access modifier

Why does the main method have to be public? The best way to answer this question is by explaining why it cannot use any of the other access modifiers like private, protected or default.

 

private – The JVM code that will invoke our main method is not inside our MainClass (the class that contains the main method), and that is why if we use the private access modifier the JVM will not be able to access the main method hence it will not be able to execute our program.

 

If we declare the main method private compilation will succeed, but it will fail at runtime because the JVM will not be able to locate our main method.

 

Consider the following Java program where we changed the main method access modifier from the public to private:

 

 
public class MainClass { 
   private static void main(String[] args) { 
      System.out.println("Hello world!"); 
   } 
} 

 

When we attempt to compile and run this class, the compilation succeeds, but we will get an error at runtime like this:

 


user@computer$ javac MainClass.java
user@computer$ java MainClass
Error: Main method not found in class MyClass, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

 

protected – The JVM does not extend (inherit from) our MainClass and a protected member can be accessed only through inheritance hence we cannot use protected access modifier. If we do, we will get a runtime Error as we got for private access modifier above.

 

default – The JVM code is in a different package than ours hence we cannot use the default access modifier (default access modifier is when you do not specify any of the three access modifiers). If we use the default access modifier, we will get a runtime Error as we got for private access modifier above.

 

static – modifier

I will explain static modifier in greater details in a later tutorial, but for now, I will focus on only one aspect of it when we mark a member of a class with the static modifier that member does no longer belong to a particular object that you will create from that class, but it belongs to the class itself.

 

And if we go back to our main method it does make sense why it is declared as static. Why?

 

Because if we do not declare the main method static then we need an object to be able to run it and since there is no object when the JVM start executing our program there is no way for the JVM executes it.

 

If we do not declare the main method static as follows:

 

 
public class MainClass { 
   public void main(String[] args) { 
      System.out.println("Hello world!"); 
   } 
} 

 

and we compile and run our program we will not get a compilation error, but we will get a runtime error like this:

 


user@computer$ javac MainClass.java
user@computer$ java MainClass
Error: Main method is not static in class MainClass, please define the main method as:
public static void main(String[] args)

 

void – return type

This is the main method return type. What void means for a method? Is that the method does not return anything. And it does make sense for the main method not to return anything because when the main method completes the program terminates and if we return anything will be useless because the program has already terminated.

 

main – method name

In regards to the name of the method, there is not much to say. The rule is that it has to be named exactly like this: “main” (It has to be lower case. Remember main is different from Main).

 

String[] args or String… args – method parameters

As you know when you run a java program from the command line, for example, you can specify arguments, and these arguments will be used by the JVM to pass them to the main method. Here is an example of the main method that uses these parameters:

 

 
public class MainClass {
   public static void main(String[] args) { 
       System.out.println(args[0]);
       System.out.println(args[1]);
       System.out.println(args[2]); 
   }
} 

We can compile and run this simple java program as follows:

 


user@computer$ javac MainClass.java
user@computer$ java MainClass FirstParameter SecondParameter ThirdParameter
FirstParameter
SecondParameter
ThirdParameter

 

If we change String[] args to String… args we should get the same output.

 

Try it yourself:

 

Change the String[] args with String… args, recompile and run. Compilation should be successful, and the output should be the same.

 

We so that String[] and String… have the same functionality and the only difference is the syntax which leaves us with only one more aspect: “args” identifier.

 

This identifier can be any valid Java identifier, even though most people will use the args that stand for arguments. Java identifier example that you can use here:  String[] agrumentsArray.

 

I hope this clarify Java’s main method used.

 

I would appreciate if you will live your comments and let me know if I can/need to improve this post.

 

Thank you very much, and I hope you enjoyed reading this post.