Monday 6 August 2012

Meaning of public static void main ( String args[] ) in Java



public static void main( String args[] )


This is main( ) method. This is the line at which program begins execution.
The meaning of each part of this line is given below:

public :

The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. In this case, main( ) must be declared as public, since it must be called by code outside of its class when the program is started.


static :

The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. Hence to call main( ) there is no need to create an object of the class, it can be directly called. This is necessary since main( ) is called by the java interpreter before any objects are made.


void :

The keyword void simply tells the compiler that main( ) does not return a value.


main() :

main( ) is the method called when a java application begins. Since java is case sensitive main is different from Main. 


String args[] :

Any method that you need to pass to a method is received by variables specified within the set of parenthesis that follow the name of the method. These variables are called parameters. If there are no parameters required for a given method, you still need to include the empty parenthesis. In main( ) there is only one parameter. String args[] declares a parameter named args, which is an array of instances of the class String. In this case, args receives any command line arguments present when the program is executed.





1 comment: