| "A method signature is a collection of information about the method, as in a C prototype or a forward function declaration in other languages. It includes the method's name, type, and visibility, as well as its arguments and return type." |
In brief, the method signature can probably be thought of as providing all the information there is about the interface to the method. In other words, it provides all the information that you need to know to be able to invoke the method.
The controlling class of every Java application must contain
a main() method having one of the following signatures (this author
prefers 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 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.
Since args is a true array object, it has a parameter named length. The args.length variable can be used by the code in the program to determine the number of arguments actually entered. The runtime system monitors for the entry of command-line arguments by the user and constructs the String array containing those arguments.
If the array of strings exists, the first string in the array corresponds to the first argument (not the name of the program as in C++).
Command-line arguments along with strings and String arrays will
be discussed in more detail in a subsequent lesson.
A - public static void main(String[] args)
Q - Briefly explain the reason that the main method in a Java application is declared public.
A - The keyword public indicates that the method can be called by any object.
Q - Explain the reason that the main method in a Java application must be declared static.
A - 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.
Q - Describe the purpose of the keyword void when used as the return type for the main method.
A - The void keyword when used as the return type for any Java methods indicates that the method does not return anything.
Q - As in C++, if the Java application is not designed to use command-line arguments, it is not necessary to include a formal parameter for the main method which is an array of type String: True or False?
A - False. Although C++ allows the formal argument list to be omitted from a main method that is not designed to support command-line arguments, the main method in a Java program must always provide the formal argument list regardless of whether it is actually used in the program.
Q - When using command-line arguments in Java, if the name of the string array is args, the args.length variable can be used by the code in the program to determine the number of arguments actually entered: True or False?
A - True
Q - As in C++, the first string in the array of command-line arguments contains the name of the Java application: True or False?
A - False. Unlike C++, the first string in the array of command-line arguments in a Java application does not contain the name of the application.
Q - The controlling class of every Java application must contain a main method. Can other classes in the same application also have a main method? If not, why not? If so, why might you want to do this?
A - Yes. It is often desirable to provide a main method for a class that will not ultimately be the controlling class in an application to allow the class to be tested in a stand-alone mode, independent of any other classes.