Saturday, April 2, 2011

The main() Method in Java

Signature of main() Method

The controlling class of every Java application must contain a main() method having one of the following signatures (the first signature as being the most descriptive of an array of String references which is what is passed in as an argument).

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


The keyword public indicates that the method can be called by any object. 


The keyword static indicates that the method is a class method, which can be called without the requirement to instantiate an object of the class. This is used by the Java interpreter to launch the program by invoking the main method of the class identified in the command to start the program.


The keyword void indicates that the method doesn't return any value.


The formal parameter args is an array of type String, which contains arguments entered at the command line. Note that the args parameter must be specified whether or not the user is required to enter a command-line argument and whether or not the code in the program actually makes use of the argument.

No comments:

Post a Comment