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?

Last Updated on December 27, 2023 by Ankit Kochar

The main method stands as the entry point for any Java application. It serves as the starting point for the Java Virtual Machine (JVM) to execute the code. Understanding the significance of the main method, particularly its static nature, is fundamental for Java developers. In this article, we’ll delve into the core concept of why the main method is static in Java, its implications, and its role in Java applications.

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 conclusion, the static nature of the main method in Java is a pivotal aspect of the language’s design. Its requirement as a static method ensures that it can be accessed without creating an instance of the class, making it the initial point for the JVM to commence execution. By adhering to the specific signature and properties, the main method remains consistent across Java applications, simplifying the startup process and maintaining a standardized entry point. Its static nature aids in the seamless initiation of Java programs, playing a crucial role in the language’s architecture and execution flow.

Understanding the importance of the main method being static not only provides clarity in programming but also aids developers in crafting efficient, functional, and organized Java applications.

Frequently Asked Questions (FAQs) Related to Main Method is Static in Java

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

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

2. Can I change the signature of the main method in Java?
No, the signature of the main method in Java is standardized and should strictly adhere to the format public static void main(String[] args). Altering this signature will result in the JVM failing to recognize the method as the program’s starting point.

3. What happens if the main method is not declared as static in Java?
If the main method is not declared as static in Java, the JVM won’t be able to invoke it directly without an instance of the class containing the main method. Consequently, the Java program won’t execute as expected and will likely throw a runtime error.

4. Is it possible to have multiple main methods in a single Java class?
While it is possible to declare multiple methods named "main" within a class, only the method with the signature public static void main(String[] args) will serve as the entry point when the program is executed. Other methods named "main" won’t be recognized as the starting point by the JVM.

5. What is the purpose of the String[] args parameter in the main method?
The String[] args parameter in the main method allows for the passing of command-line arguments to the Java program. It enables developers to input values or configurations at runtime, providing flexibility and customization to the application’s behavior.

Leave a Reply

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