2.6 The main() Method


2.6 The main() Method

The mechanics of compiling and running Java applications using the Java 2 SDK are outlined in Section 1.10. The Java interpreter executes a method called main in the class specified on the command line. Any class can have a main() method, but only the main() method of the class specified to the Java interpreter is executed to start a Java application.

The main() method must have public accessibility so that the interpreter can call it (see Section 4.9, p. 138). It is a static method belonging to the class, so that no object of the class is required to start the execution (see Section 4.10, p. 144). It does not return a value, that is, it is declared void (see Section 5.4, p. 176). It always has an array of String objects as its only formal parameter. This array contains any arguments passed to the program on the command line (see Section 3.23, p. 95). All this adds up to the following definition of the main() method:

 public static void main(String[] args) {     // ... } 

The above requirements do not exclude specification of additional modifiers (see Section 4.10, p. 144) or any throws clause (see Section 5.9, p. 201). The main() method can also be overloaded like any other method (see p. 116). The Java interpreter ensures that the main() method, that complies with the above definition is the starting point of the program execution.



A Programmer[ap]s Guide to Java Certification
A Programmer[ap]s Guide to Java Certification
ISBN: 201596148
EAN: N/A
Year: 2003
Pages: 284

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net