The main method

In the last lesson, you learnt about a class construct. A class can contain other Java constructs, off which, a method is one of them.

Just like a class can be named what ever you want, same way, a method can be named what you you want too.

However, if you name your method main and also define the rest of the method definition as shown below, then this will become 'The main method' of the Java class.

This main method is coded as:

public static void main(String[] args) {
    // other java code go here. Here is an example statement

In the Java programming language, every standalone application must contain a main method whose signature is:

public static void main(String[] args)

The modifiers public and static can be written in either order (public static or static public), but the general practice is to use public static as shown above. You can name the argument anything you want, but naming it ‘args’ is again a general practice. The main method accepts a single argument of type String array.

The entire line of the method definition, which includes all the modifiers, the method name and its arguments is called the method signature -

public static void main(String[] args)

You will learn more about String and Arrays in the subsequent chapters.

When a program has a main method, you can run that program from command line. The main method is the entry point for your application and all other methods are invoked from the method method. In our simple example, you only have a System.out.println statement and there were no other methods. So the program started the execution from the main method and executed System.out.println statement which printed out "hello" in the output.

Program Arguments

The single argument that the main method accepts is a way through which you can pass information to your application during runtime. These arguments are also called program arguments. If you had to run the Tutorial program from command line then you would open a terminal (a.k.a. console) and invoke JRE's java executable by keying in:

java Tutorial arg1 arg2 arg3

arg1, arg2, and arg3 are the program arguments (a.k.a. command-line arguments). You can pass zero to many program arguments. arg1/2/3 are just placeholders for sending any values to your program.


A standalone application is made of one or more java files in which at least one file will have the main method. A user can start the application from the class which contains the main method using JRE's java executable program.

Note: Main method is a special type of method. You will learn more on methods in general later. However for the next few lessons we will only focus on using the main method only.

static, public keywords

If you are wondering what the static and public mean?, then for the time being we will ignore finding the answer. Just remember that the main method should have both static and public only then it will work. You will learn more about these keywords in detail in the next module.

What is a statement?

Let us look at the line:

System.out.println("hello " + args[0] + "!");

This line is called a Statement and every statement ends with a semicolon(;). A Java program is made up of many different statements. In the example statement, we use the System class from the core library to print the "hello world!" message to the standard output (a.k.a. console).

"hello world!" is constructed from String value “hello” which is concatenated with program argument received through args[0] and an exclamation mark. Multiple Strings can be concatenated using plus (+) operator.

Remember to add a space after hello to get the above output



  • A statement always ends with a semicolon - ;
  • Observe the indentation applied to the second line. It is a convention to apply indentation to statements within a block of code. A code block is anything within a set of two curly braces {}.
  • The compiler does not care if you indent or not. You apply it so that humans can understand the code better. Some of the advanced IDE's like Android Studio, automatically indent the code for us.

results matching ""

    No results matching ""