Skip to content Skip to navigation

OpenStax-CNX

You are here: Home » Content » Jb0180: Java OOP: The main method.

Navigation

Recently Viewed

This feature requires Javascript to be enabled.
 

Jb0180: Java OOP: The main method.

Module by: R.G. (Dick) Baldwin. E-mail the author

Summary: Every Java application requires a class containing a method named main. This module provides information on the main method.

Note: You are viewing an old style version of this document. The new style version is available here.

Preface

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.

Viewing tip

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.

Images

  • Image 1 . The method signature according to Niemeyer and Peck.
  • Image 2 . The method signature according to Oracle.
  • Image 3 . Allowable signatures for the main method.

The main method in Java

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 .

Table 1
Image 1 . The method signature according to Niemeyer and Peck.

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

Table 2
Image 2 . The method signature according to Oracle.

Definition: Two of the components of a method declaration comprise the method signature--the method's name and the parameter types.

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 .

Table 3
Image 3 . Allowable signatures for the main method.

public static void main(String[] args)

public static void main(String args[])

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

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Jb0180: Java OOP: The main method.
  • File: Jb0180.htm
  • Originally published: 1997
  • Published at cnx.org: 11/17/12
  • Revised: 12/04/14
Disclaimers:

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-

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks