Why main method is declared as static in java?

Why the main method is declared as static in java?

As you know in a simple java program, we declared the main method as following way

**public static void main(String args[])

here the method name is main.

By using 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 NoClassFoundError exception.
if the main method is 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()),
For the main method, we are declaring the method as static, means JVM can access the main method directly using the direct class name. That’s why for being the main method declared as static, object creation is not created.

Void in the main method returns nothing.

public keyword means any other outside all classes can be accessed.

Strings Args[]:- arguments are command line arguments for the java class. we can set arguments through command line as part of 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.

Please feel free to comment if you like this post if you have any questions.


I am a fresher in java and this site has some very…

Unknown - Jun 4, 2012

I am a fresher in java and this site has some very helpful pack of articles! Thanks and please keep updating! Sample Statements

Similar Posts