Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

main method in Java

Tags: main java static
In this page, we will discuss about Java Main method and why main method has public static void. What does the main method in java? we will learn all these things in this page. Every Java Developer should have knowledge on this concept.

Whether the class contains main() method  or not and whether main() method properly declared or not . These checking are not responsibility of compiler. But at run time JVM is the responsible to perform these checking. In case JVM unable to find require main method then we will get runtime exception saying NoSuchMethodError:main 

Example:

class Test
{
}
Output:




JVM always searches for the main() with the fallowing signature only:



In the above code begins defining main() method. This is the line where program will start execution.

public:
 In the main() method signature the first one that is public keyword is an access specifier,which allows the programmer to control  visibility of the class members.That means it is called by JVM from any where. So, must be declare main() method as public,because it must be called by code outside of its class.



static:
Every java program's main() method has to be declared static , since the keyword static allows without existing object also JVM has to call this method. If you don't declare static keyword in method signature program will compile successfully but at run time it will report an error.

void:

The keyword void tells the compiler will not return anything to JVM.

main:

It is name of the method which is configured inside JVM. Java is a case-sensitive language that means Main() is different from main() method.So, you must declare main() method as specified in Java Programming language.

String[] args:

String [] args means in main() method signature is array of Strings. Here args contains the supplied command line arguments as an array of String objects.
Instead of args we can take any valid java identifier. We can replace String[] with var-arg parameter
Example:
public static void main(String...args)

Note: 
we can change the order of modifiers that is instead of public static,we can take static public

I hope you enjoy this page and follow me more updates and write your comments with your valuable answers.





This post first appeared on Learnprogramingbyluckysir, please read the originial post: here

Share the post

main method in Java

×

Subscribe to Learnprogramingbyluckysir

Get updates delivered right to your inbox!

Thank you for your subscription

×