Copyright

What is a Main Method in Java? - Definition & Purpose

Instructor: Thomas Wall

Thomas is a professional software developer, online instructor, consultant and has a Masters degree.

In this lesson you will learn about the special Java method named 'main', the meaning of the arguments passed into it, and how to use them to get your program running smoothly.

Main Street, Javaville

Almost every town, large or small, has a main street, which is generally the place where the town began. Every Java program, large or small, has a method named main, where the execution of your program logic begins. Just as preliminary work needs to be done to build a main street, there is preliminary work that needs to done by the operating system, such as loading the Java Virtual Machine (JVM) and your compiled Java code into memory. Once that's completed, control is passed to a method called main. The code you place in main determines the behavior of your program from this point on.

Main Method Structure

The declaration of the Java main method is:

public static void main(String[] args) {
  //Your code goes here
}

It must be declared 'public static' so it's initially loaded into memory and accessible to any caller (such as the JVM) in your program's global namespace. The use of 'void' indicates the main method doesn't need to return a value to the caller. 'String[] args' means the caller passes a reference to an array of String objects into your main method, and the array can be accessed using the local variable named args. The number of strings held in the args array can be determined via args.length, which will be zero if the array is empty. If the array isn't empty, args[0] refers to the first String object, args[1] to the second, args[2] to the third, and so on.

Main Method Arguments

What does the array of text String objects represent and where does it come from? Like most computer programs, a Java program is usually started by typing something on a command line. Usually the name of the file containing the compiled Java code is specified first, followed by a zero or more option names, each separated by one or more spaces. If one of the option names itself contains spaces, that option name must be enclosed in double quotes. Here's an example command line used to launch a Java program named SortWordFile, which will sort the words (one per line) contained in an input file storing the sorted results in an output file. This program needs to know three things in order to do its job:

  • Name of the input file.
  • Name of the output file.
  • Whether to sort the names in ascending or descending alphabetic order.

For illustration purposes assume the input file is named 'words.txt', and we want the output file named 'sortedwords.txt', sorted in descending alphabetic order. On many operating systems (such as Windows or Linux) our command line might look like this:

java SortWordFile words.txt sortedwords.txt descending

The first two words (java SortWordFile) are needed to tell the operating system we want to load and execute a Java program stored in the file SortWordFile. These words are not considered arguments needed by the program, so they are not passed to the main method. The remaining three space separated words are considered options to the program and are passed to the main method as args[0] = 'words.txt', args[1] = 'sortedwords.txt', and args[2]= 'descending'.

Example Usage

Here's an example of what the main method for the SortWordFile program might look like:

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support