Summary: Every Java application requires a class containing a method named main. This module provides information on the main method.
This module is part of a sub-collection of modules designed to help you learn to program computers.
Every Java application requires a class containing a method named main . This module provides information on the main method.
I recommend that you open another copy of this module in a separate browser window and use the following links to easily find and view the images while you are reading about them.
There must be a main method in the controlling class in every Java application.
The method signature
The Java literature frequently refers to the signature of a method, or the method signature .
Exploring Java by Patrick Niemeyer and Joshua Peck (O'Reilly) provides the definition of a method signature shown in Image 1 .
| Image 1: The method signature according to Niemeyer and Peck. | |
|---|---|
|
Type
Apparently in this definition, the authors are referring to the type of the method as distinguishing between static and non-static. (Other literature refers to the type of a function or method as being the return type which according to the above definition is a separate part of the signature.)
Visibility
Apparently also the use of the word visibility in the above definition refers to the use of public , private , etc.
According to Oracle...
Oracle's Java Tutorials , on the other hand, describe the method signature as in Image 2 .
| Image 2: The method signature according to Oracle. | |
|---|---|
|
As you can see, the Oracle definition is more restrictive than the Niemeyer and Peck definition.
Bottom line on method signature
The method signature can probably be thought of as providing information about the programming interface to the method. In other words, it provides the information that you need to be able to call the method in your program code.
Signature of main method
The controlling class of every Java application must contain a main method having one of the signatures shown in Image 3 .
| Image 3: Allowable signatures for the main method. | |
|---|---|
|
(I prefer the first signature in Image 3 as being the most descriptive of an array of String references which is what is passed in as an argument.)
public
The keyword public indicates that the method can be called by any object. A future module will discuss the keywords public , private , and protected in more detail.
static
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 JVM to launch the program by calling the main method of the class identified in the command to start the program.
void
The keyword void indicates that the method doesn't return any value.
args
The formal parameter args is a reference to an array object of type String . The array elements contain references to String objects that encapsulate String representations of the arguments, if any, entered at the command line.
Note that the args parameter must be specified whether or not the user is required to enter command-line arguments and whether or not the code in the program actually makes use of the argument. Also note that the name can be any legal Java identifier. It doesn't have to be args . It could be joe or sue, for example.
The length property
The parameter named args is a reference to an array object. Java array objects have a property named length , which specifies the number of elements in the array.
The runtime system monitors for the entry of command-line arguments by the user and constructs the String array containing those arguments.
Processing command-line arguments
The args.length property can be used by the code in the program to determine the number of arguments actually entered by the user.
If the length property is not equal to zero, the first string in the array corresponds to the first argument entered on the command line.
Command-line arguments along with strings and String arrays will be discussed in more detail in a future module.
This section contains a variety of miscellaneous information.
Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.
I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.
Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.
-end-