Richard G Baldwin (512) 223-4758, baldwin@austin.cc.tx.us, http://www2.austin.cc.tx.us/baldwin/

The main() Method

Java Programming, Lecture Notes # 14, Revised 10/03/99.

Preface
Introduction
The main() Method in Java
The main() Function in C++
Review Questions for Lesson 14

Preface

Students in Prof. Baldwin's Introductory Java Programming classes at ACC are responsible for knowing and understanding all of the material in this lesson (except that they are not responsible for detailed information that is specific to C++).

The detailed material on C++ is provided as supplementary material for the benefit of those persons who are already familiar with C++ and who are making the transition into Java.

Introduction

The main() Method

There must be a main method in the controlling class in a Java application. There must also be a main function in a stand-alone C++ program.

The main() Method in Java

The Method Signature

The Java literature frequently refers to the signature of a method, or the method signature.

Exploring Java by Patrick Niemeyer & Joshua Peck (O'Reilly) provides the following definition of a method signature.
 

"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."

Type

Apparently in this definition, they are referring to the type of the method as distinguishing between static and non-static (other literature refers to the type of a function 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.

Bottom Line on Method Signature

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.

Signature of main() 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[])

public

The keyword public indicates that the method can be called by any object. A subsequent lesson 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 Java interpreter to launch the program by invoking 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 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.

The length Property

In Java, args is a true array object. Array objects have a property named length.

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 array of strings contains data, 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.
 

The main() Function in C++ 

The main function in a C++ program can have either of the following signatures: 
 

return-type main()
return-type main(int argc, char *argv[])

The return-type can be any valid type or void indicating that the function does not return any value. The argument list can be empty, or can contain the arguments shown to support the use of command-line arguments. 

The argument int argc is used by the operating system to pass an integer value specifying the number of command-line arguments entered by the user.

The argument char *argv[] passes a pointer to an array of pointers of type char. Each of the pointers in the array points to a null-terminated string in memory, which is an argument, entered at the command line. The pointer with an index of 0 points to a string containing the name of the program (or garbage with some systems). The pointer with an index of 1 points to the first true command-line argument. 

The names of the arguments shown are traditional, but not required. 

.

Review Questions for Lesson 14

Q - Write the method signature for the main method in a Java application.

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.

-end-