{

Why the main method is declared as static in java?


In a simple java program, we declared the main method as the following way.

public static void main(String args\[\])  {

}

The method name is main

By using the java filename command, JVM loads the Java class into the memory and looks for the main class in the java file.

if the main method is not found, it throws the NoClassFoundError exception.

if the main method found, it will start the java execution code process.

if you want to access any method, we can use method name with an object (like object.method()),

We declare the main method as static, which means that JVM can call the main function directly using the direct class name.

As a result, object creation is bypassed because the main method is declared static.

Void in the main method returns nothing.

public keyword means public to all which means outside all classes can be accessed.

Strings Args\[\]:- arguments are command-line arguments for the java class. we can set arguments through the command line as part of the Java tool to send some arguments while executing a java program.

usage :- java javafilename argument1 argument2  

arguments1,argument2 are available in string args[], once execution starts with the above command.

Hope you understand the basic usage of the main method.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.