Saturday, 20 December 2014

public static void main(String args[])


---------------------------------------------------------------------------------------------

In Java, the execution starts from main() method. But for compilation, main() is not required. Java's main() method syntax is quiet different from C/C++.

Following is the main() method signature
public static void main(String args[])
Every word in the above statement has got a meaning to the JVM.
1. public: It is a keyword and denotes that any other class (JVM) can call the main() method without any restrictions.
2. static: It is a keyword and denotes that any other class (JVM) can call the main() method without the help of an object. More about static is available at static Keyword – Philosophy.
3. void: It is a keyword and denotes that the main() method does not return a value.
4. main(): It is the name of the method.
5. String args[]: The parameter is a String array by name args. The string array is used to access command-line arguments.

The Java program under execution takes some context area (execution area) in the RAM. JVM is in another context area. To call the main(), the JVM requires an object. When the JVM has not entered into the program, how it can create an object or get an object. To overcome this, allow the JVM to access the main() without object just by declaring static. Once the JVM enters, it can create hundreds of objects later.

Whether you pass right now command-line arguments or not, you must have the string array as parameter as it is the part of syntax.

0 comments

Post a Comment