Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Reasons behind Defining the main() Method as Static
2.
Main Method in Java
3.
Public Static Void Main(String[] args)
3.1.
Public
3.2.
Static
3.3.
void
3.4.
main
3.5.
String[] args
4.
Frequently Asked Questions
4.1.
Is main() method always static in Java?
4.2.
Can the main() method be private?
4.3.
How to make the main() method not static in java?
4.4.
Who calls main() method in Java?
5.
Conclusion
Last Updated: Jun 5, 2024
Medium

Why Main Method is Static in Java?

Author Rashi
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

The main() method in Java is static so that the JVM may call it without creating an instance of the class that contains the main() function. This allows the JVM to start the execution of the program without needing to instantiate any objects. By declaring main() as static, we allow the JVM to load the class into memory and call the main() method directly.

The main() method in Java programs is the point from which the program begins its execution or simply the entry point of Java programs. In conclusion, it is one of the most crucial Java methods, and a thorough understanding of it is critical.

main method is static in java

When a Java program is executed, the Java compiler or JVM looks for the main method. For the JVM to recognize the main method as its entry point, its signature must be in a specific format. If we change the method signature, the program compiles but does not run. In this blog, we will learn why the main method is static in java.

Reasons behind Defining the main() Method as Static

Here are some of the reasons for the main method in Java being static.

  • The Java Virtual Machine can call it without having to create an instance of the class that contains it. That's why the main method is static in Java.
  • Because C and C++ both have a similar main method that serves as the starting point for program execution, adhering to that convention will only benefit Java.
  • JVM would have to create an instance of the main Class, and because the function Object() [native code] can be overloaded and have arguments, there would be no certain and more consistent way for the JVM to find the main method in Java, that's why the main method is static in Java.
  • If the main method is not declared static, the JVM must create an instance of the main Class, and because the constructor can be overloaded and have arguments, there is no certain and consistent way for the JVM to find the main method in Java.
    Now we know why the main method is static in Java, what are the reasons behind it and also what each keyword means in the syntax while we write the main statement.

Main Method in Java

The java.exe program is used to execute the Java programs. Java.exe then makes Java Native Interface (JNI) calls, which load the JVM. The command line is parsed by java.exe, a new String array is created, and the main() method is called. The main method is attached to a daemon thread, which is destroyed only when the Java program terminates execution.

The main() method is the first one found during execution. As a result, it is a program's starting point. In this article, we will learn why main method is static in Java.

The syntax of the main() method cannot be changed. The only thing we can change is the String array argument's name. The main() method has the following syntax:

public class CodingNinjas {
    public static void main(String[] args) {
        System.out.println("Happy Learning!");
    }
}


Output:

Output
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Public Static Void Main(String[] args)

Let's break down the main() method's syntax into its various components and examine each one separately.

Public

It is easy to comprehend. It is a main() method access modifier. We define the main() method with a public access specifier so that it can be executed by any program. As a result, we must make the main() method public, and if we make it non-public, the following error will occur:

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


Output:

Output Public

Static

The main() method is designated as static to allow the JVM to invoke it without needing to instantiate the class that encompasses the main() function. As there is no class object present upon the start of the Java runtime, it is necessary to declare the main() function as static.

So, if we define the main() method as non-static, JVM will be unable to call it and will throw the following error:

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


Output:

Output Static

void

Each method, as we know, has a different return type, such as String, Boolean, Integer, and so on. The Java main() method uses the void return type, which does not return anything. To keep things simple, the main() method returns nothing. After executing the main() method, the program will be terminated, and returning anything from the main() method is useless because the JVM will do nothing with the returned object.

If we return something from the main() method, the following error will occur:

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


Output:

Output Void

main

It refers to the main() method. The method's name is fixed and cannot be changed. If we attempt to alter the name of the method(), the following error will be thrown:

public class CodingNinjasTest {  
        // do not define main() method as static 
        public static void changedMain(String[] args){  
            System.out.println("changed name of main method.");  
        }  
}


Output:

Output Main

String[] args

As a parameter, the Java main() method accepts a single String array argument, also known as a command-line argument.

Frequently Asked Questions

Is main() method always static in Java?

The compiler can call it before or after the creation of a class object, that's why main method is static in Java.

Can the main() method be private?

In Java, we can declare the main method to be private. The main method is successfully compiled without any errors, but at runtime, it complains that it is not a public method.

How to make the main() method not static in java?

In Java, to make the main method non-static, you need to create an instance of the class that contains the main method enables you to access instance-specific variables and methods.

Who calls main() method in Java?

The class's main() method is the first thing the Java interpreter calls when starting an application (invoked upon the controlling class of the application). All the other methods needed to run your application are then called by the main method.

Conclusion

We have briefly discussed in this article about the main() method and why the main method is static in Java. We have also discussed all the parts of the syntax of main() method "public" is the access specifier, "static" is the keyword," void" is the return datatype, and "main" is the method name.

Recommended Readings:

Also learn the basics of Java and Data structures and algorithms.

You can refer to our guided paths on Code 360 to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Previous article
Static and Instance Methods in Java
Next article
Method Overriding In Java
Live masterclass