Java main method Explained [Easy Examples]

 

Introduction to Java main method

The java main method is a standard method that is used by JVM to start the execution of any Java program. The java main method is referred to as the entry point of the Java application. In this tutorial, we will learn about the java main method. We will see how we can create the java main method and explain its every component in detail.

We will also discuss how we can pass different arguments to the java main methods by taking various examples. Moreover, we will also learn the overloading of the java main method. All in all, this is going to be one of the most informative tutorials about the java main method.

Advertisement

 

Getting started with Java main method

As we already discussed the java main method is the entry point for any Java application. The main method will subsequently invoke all the other methods required by the program. In this section, we will discuss the syntax and different ways of writing the java main method and will explain each of its components in more detail.

 

Different Java main method syntax

The java main method can be written in many ways depending on the situation. Although they're not very commonly used, they're valid signatures. Note that none of those are specific to the main method, they can be used with any Java method but they are also a valid part of the main method.

 

Syntax-1 to use java main method

Let us see some of those methods here. See the simple syntax of writing the java main method.

public static void main(String[] args) {
    // statements
}

 

Syntax-2 to use java main method

The above one is the most commonly used syntax of the java main method. Notice that the square brackets are just after the String but they can also be written after args as well. See the syntax below:

public static void main(String args[]) {
    // statements
}

This is a valid declaration of the java main method.

 

Syntax-3 to use java main method

Another way of writing the main method is to add strictfp with it, which is used for compatibility between processors when working with floating-point values. See the simple syntax below:

Advertisement
public static strictfp  void main(String[] args) {
    // statements
}

 

Syntax-4 to use java main method

Another way to write the java main method is to write the final inside the main method which can be applied on args to prevent the array from being modified. See the simple syntax below:

public static void main(final String[] args) {
    // statements
}

Notice that the above all are valid but not commonly used methods of declaration, the very first one is the most popular one and we will also use that syntax in the rest of our tutorial.

 

Different ways of using Java main method

Understanding the "public" keyword

The public is a Java keyword that declares a member's access as public. Public members are visible to all other classes. This means that any other class can access a public field or method. Further, other classes can modify public fields unless the field is declared as final. The main method in java is public so that it can be accessible everywhere and to every object which may desire to use it for launching the application.

Notice that if we will not make the main method public, then there will be no compilation error. But we will get runtime error because matching the main method is not present. Remember that the whole syntax should match to execute the java main method. For example, see the example below:

java main method

Notice that it says the main.java is not executable, that is because our main method is not public.

 

Understanding the keyword "static"

In the Java programming language, the keyword static indicates that the particular member belongs to a type itself, rather than to an instance of that type. This means that only one instance of that static member is created which is shared across all instances of the class. In simple words, static methods are the method that invokes without creating the objects, so we do not need any object to call the main method. If we will not make the main method static we will get the following error. See the example below:

// class
public class Main  { 
    // main method withou static keyword
    public void main(String args[]){
       System.out.println("golinuxcloud!!");  
    }
}

Output:

java main method error

Notice that it says the main method is not found and gave us the proper syntax of the main method.

Advertisement

 

Understanding the keyword "void"

The void keyword specifies that a method should not have a return value. In java, every method should have a return type otherwise we will get an error. The main method's type is void which means that it is not returning anything. If we create the main method without any return type (void) we will get the following error.

java main method void

 

Understanding the "string arg[]" in the main method

The java main method can also accept some data from the user. It accepts a group of strings, which is called a string array. It is used to hold the command line arguments in the form of string values. The agrs[] is actually an array name, and it is of String type. It means that it can store a group of strings. Remember, this array can also store a group of numbers but in the form of a string only. Values passed to the main method are called arguments. These arguments are stored into args[] array, so the name args[] is generally used for it.

Later in this tutorial, we will take examples and see how we can pass arguments to the main method.

 

Simple example of Java main method

Now we are familiar with the terms used in the java main method, we are good to go and create a main method and run it. See the example below which prints out a message.

// class
public class Main  { 
    // main method
    public static void main(String args[]){
        // message
       System.out.println("welcome to golinuxcloud java tutorials!");  
    }
}

Output:

welcome to golinuxcloud java tutorials!

 

Passing arguments to Java main method

We can pass the arguments to the java main method without getting any errors. To do that we have to first understand how to call/execute a java file from the terminal using command lines.

 

Running Java file from using commands

To run the java file from the terminal, first, open the terminal and then using cd command and folder name, go the directory where the Java file is saved. Once you are in the same directory then use the following commands to run the java file. See the syntax below:

java className.java

For example, let us say we have the following java program.

// class
public class Main  { 
    // main method withou static keyword
    public static void main(String args[]){
        // message
       System.out.println("Welcome to go linux cloud");  
    }
}

Now we will use the command line to run this program. See the result below:

command line

Notice that the java program was run successfully.

 

Passing arguments to java main method through command line

We already learned how to run a java program using the terminal and commands. Now, we are good to pass arguments to the java main method using the terminal. The following is the simple syntax of passing arguments to the main method using the command line.

java className.java arguments

Now let us say we have the following java program which prints out the arguments of the main method. See the example java program below:

// class
public class Main  { 
    // main method withou static keyword
    public static void main(String args[]){
        // printing arguments of main method
       System.out.println(args[0]);  
       System.out.println(args[1]);

    }
}

Now let us run the program using the command line. See the result below:

command line run

Notice that we successfully passed a string argument to the java main method and prints out using indexing.

 

Calling another method from Java main method

Now, we already learned all the basic things that we need to know in order to start working with the java programming language. Let us now take an example of the java main method which call another method and execute it. See the example below:

// class
public class Main  { 
    // main method withou static keyword
    public static void main(String args[]){
        // calling another method
        System.out.println(welcome());
    }
    // java method
    public static String welcome(){
        // return string type message
        return "welcome to golinuxcloud!!";
    }
}

Output:

welcome to golinuxcloud!!

Notice that the execution of the java program starts from the main method and the code inside the java main function will execute line by line. If we will not call a function inside the main method, then the function will not be executed.

 

Summary

A Java main method is the entry point of any java program. Its syntax is always public static void main(String[] args); . In this tutorial, we learn everything that you need to know about the java main method. We learned different ways to write the main method along with taking examples. At the same time, we also discussed the terms that are associated with the main method, for example, public, static, void, and string args[].

We also covered how we can run a java file from the terminal and how we can pass arguments to the java main method by taking various examples. Furthermore, we also learned how we can call another method from the main method. All in all, this tutorial contains all the necessary information that is required to start with the java programming language.

 

Further Reading

Java main method
More about main method
Java methods

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment