Java main method

What is the main method in Java?

The main method in Java is really the main method. It is the entry point of any Java program. When we want to execute some code in a class, that class must have a main method.

The Java interpreter starts executing the application by calling the class’s main method. The main method then calls all other methods needed to execute the application.

In the body of the main method, a call to other methods is usually placed to initiate the execution of our written code. 

It is not enough to put the main method with an empty body, the program will run, but the rest of the code will not be called, so we place calls to other methods in the body of the main method.

Syntax

public class App {
  public static void main(String[] args) {
    // ...
  }
}

Let’s see what the definition of the main method contains:

  • public – This is one of the Java access modifiers. Public means that the method will be visible within the entire program structure.
  • static – This is one of the Java keywords. The static method in Java is a method that belongs to the class and not to the object.
  • void – It is the method’s return type. It means it doesn’t return any value.
  • main – the name of the method
  • String[] args – This is a parameter of the main method. Represents a command-line argument.
  • The main method accepts only one parameter: an array of strings.
  • You can change the name of the args parameter to whatever you want.

Execution of the main method

Let’s write a simple program.
Open Notepad or any other text editor and type the following code:

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

Save this file as App.java

Inside the method’s body, we have a Java for-loop that will go through the elements of the args array and print them.
You will see later what arrays are and how we can use them.

Now let’s compile the code and execute it.

Open your command prompt or terminal window, go to the location where you saved the App.java file and execute the following two commands:
javac App.java
and
java App

Now that you have executed these two commands, pay attention to the output:

compile and run java program

But wait, we didn’t get any output. What happened?

The thing is that we did not pass any parameter that the main method would accept and print.

Now let’s execute the code by entering the word Hello as a parameter.

Execute java App Helloand voila, we got the word Hello as a program output:

run java program

Since the parameter of the main method is an array of strings, it means that we can pass as many parameters as we want.

Execute java App Hello World!, and the output should be like this one:

java program output

As you can see, no matter how many words we pass, Java for-loop will go through each element of the args array and print it.

Valid Java main Method Signature

public static void main(String[] args)
public static void main(String []args) 
public static void main(String args[])
public static void main(String… args)
static public void main(String[] args)
public static final void main(String[] args)
final public static void main(String[] args)


That’s it!

 

Leave a Reply

Your email address will not be published.