Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Why Main Method is Static in Java?

Java is one of the most popular programming languages used worldwide. One of the main features of Java is the main method, which is considered a special method that is used as the entry point for a Java program. In Java, the main method is declared static which means that it belongs to the class and not to any instance of the class. In this article, we will explain why the main method is static in Java. Before answering the question, let us first learn about the main method in Java.

main() Method in Java

In Java applications, the main() method is the point at which the program begins its execution, commonly known as the entry point. When you run a Java application, the Java compiler or JVM looks for the main method.

The signature of the main method must be in a specified format for the JVM to identify it as its entry point. If we change the signature of the main method in Java, then the program can build but it can’t be run.

Java applications are executed using the java.exe program. After then, Java.exe performs Java Native Interface (JNI) calls, which load the JVM. Java.exe parses the command line, creates a new String array, then calls the main() function. The main function is linked to a daemon thread, which is killed only when the Java program’s execution is terminated.

Signature of main() Method in Java

public static void main(String[] args){
     // Statement 1
     // Statement 2
     ……
}  

The only thing which we can change in the signature of the java program is the “String arrays arguments name”.

Let us understand the various components of the main() Method one by one.

public

It is an access modifier that determines whom and from where the main() method can be accessed. We declare the main() function as public so that it can be accessed by any program (globally available). It is declared public because even if it is not part of the current class and can be called from outside the class. As a result, the main() function must be made public.
This example shows what happens if we don’t declare the main() Method as public.

class PrepBytes {  
    // define main() method as non-public  
    static void main(String[] args){  
    	System.out.println("Non-public Main method.");  
    }  
}

Output(Error):

The program compiled successfully, but main class was not found.
Main class should contain method: public static void main (String[] args).

In this code, we have declared a non-public main() method, so it gives out a compilation error as a result.

static

The main() method is marked static so that the JVM may call it without having to create an instance of the class that contains the main() function. Since no class object is existing when the java runtime starts, we must declare the main() function static. After that, the JVM may load the class into the main memory and call the main() function.

In addition, making the main() method static avoids the JVM from wasting memory that would otherwise be used by the object supplied just for running the main() function. The following code demonstrates what happens If we try to declare the main() method without the “static” keyword.

class PrepBytes {  
    // do not define main() method as static 
    public void main(String[] args){  
        System.out.println("Non-static Main method.");  
    }  
}

Output(Error):

The program compiled successfully, but main class was not found.
Main class should contain method: public static void main (String[] args).

Here, since we do not declare the main Method as static, the compiler is unable to find the main method. Hence, we get the above-shown compilation error.

void

In Java, each method has a unique return type, such as Integer, Double, Boolean, String, and so on. To keep things simple, a Java main() method returns nothing and has the return type void.
The application will be stopped after executing the main() function, and returning anything from the main() method is pointless because the JVM cannot do anything with the return value. If we do not provide the main() function with a void return type, the following error will be thrown.

class PrepBytes{  
    // do not define main() method with return type
    public static main(String[] args){  
        System.out.println("Main method with no return type.");  
    }  
}

Output(Error):

Main.java:3: error: invalid method declaration; return type required
    public static main(String[] args){ 

In this example, we have not declared any return type of the main Method. Since the main method always has a return type, it shows a compilation error.

main

In Java, it is the name of the main() method and is not a keyword. At the Java program’s starting point, the JVM looks for this identifier. The method must be specified as the main, and it cannot be changed.

class PrepBytes {  
    public static void changedMain(String[] args){  
       System.out.println("changed name of main method.");  
    }  
}

Output(Error):

The program compiled successfully, but main class was not found.
Main class should contain method: public static void main (String[] args).

In this example, we have named the main method “changedMain”. When we try to run the program, it shows the compilation error because the JVM could not find the main method.

String[] args

It is a java.lang.string array that accepts a single string array parameter, often known as Java command-line arguments. The name of the String array is args, but it is not mandatory and the user can give any other name as per his/her wish.

Why Main Method is Static in Java?

This section will explain in detail why the main method is static in Java.

  • The main() method is at the core of any Java application. The main() method is required because the compiler only executes a program from the main() function.
  • If the main() method is not static, the JVM should instantiate the class, which is not feasible.
  • If methods are declared as static, The JVM (Java Virtual Machine) can execute static methods by using only the class name, without producing an instance of the class.
  • The main() function must be declared static, public, and return void. If we do not define the java program as static and public, or if we do not give it a void return type, the JVM will be unable to run it and will produce an error.

Conclusion
In this article, we briefly covered the main function and why main method is static in Java. We’ve also gone through the syntax of the main() method, where "public" is the access specifier, "static" is the keyword, "void" is the return datatype, and "main" is the method name.

Frequently Asked Questions (FAQs)

Here are some Frequently Asked Questions on the main() Method in Java.

Ques 1. What are the arguments of the main method in Java?
Ans. The arguments of the main method in Java are an array of strings that represent the command-line arguments passed to the program.

Ques 2. Can we use the main method to create objects in Java?
Ans. No, we cannot use the main method to create objects in Java.

Ques 3. Can we call the main method from another method in Java?
Ans. Yes, we can call the main method from another method in Java, but it’s not recommended.

Ques 4. Can we use the main method to create objects in Java?
Ans. No, we cannot use the main method to create objects in Java.

Leave a Reply

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