The main method
In the last lesson, you learned about the class construct. A class can contain other Java constructs, one of which is a method.
Just as a class can be given any name, a method can also be named whatever you choose.
However, defining a method named main with the specific structure shown below creates the main method of the Java class.
The main method is written as:
- The
mainmethod is the starting point of any Java application. When you run a program, the JVM searches for this specific method signature to begin execution.
Task: Implementing the main method.
public static void main(String[] args)
{
// other Java code goes here. Here is an example statement:
System.out.println("hello");
}
In the Java programming language, every standalone application must contain a main method with the following signature:
public static void main(String[] args)
The modifiers public and static can be written in either order (public static or static public), but the common practice is to use public static as shown above. You can name the argument anything you want, but naming it args is also standard practice. The main method accepts a single argument of type String array.
The entire line of the method definition, which includes the modifiers, the method name, and its parameters, is called the method signature:
public static void main(String[] args)
You will learn more about String and Arrays in subsequent chapters.
When a program has a main method, you can run that program from the command line.
The main method is the entry point for your application, and all other methods are invoked from it. In our simple example, you only have a System.out.println statement and there are no other methods. The program starts execution from the main method and executes the System.out.println statement, which prints "hello" to the output.
Program Arguments
The single argument that the main method accepts allows you to pass information to your application at runtime. These arguments are also called program arguments. To run the Tutorial program from the command line, you would open a terminal (a.k.a. console) and invoke the JRE's java executable by entering:
Task: Passing arguments to a Java program from the terminal.
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, arg2, and arg3 are just placeholders for any values you send to your program.
- A standalone application is composed of one or more Java files in which at least one file contains the
mainmethod. A user can start the application from the class containing themainmethod using the JRE'sjavaexecutable.
Note: The main method is a special type of method. You will learn more about methods in general later; however, for the next few lessons, we will focus only on using the main method.
static, public keywords
If you are wondering what static and public mean, we will ignore that for now. Just remember that the main method must have both the static and public keywords to work. You will learn more about these keywords in detail in the next module.
What is a statement?
Let us look at the line:
- A statement is a complete instruction to the computer. In Java, every statement must end with a semicolon (
;).
- You can combine multiple strings or variables into a single string using the
+operator.
Task: Using System.out.println with string concatenation.
System.out.println("hello " + args[0] + "!");
This line is called a statement, and every statement ends with a semicolon (;). A Java program is composed 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 the String value "hello", which is concatenated with the program argument received through args[0] and an exclamation mark. Multiple Strings can be concatenated using the plus (+) operator.
Output
In the example above, we used System.out.println, but it is important to understand that Java provides two distinct methods for displaying information on the computer screen: System.out.print and System.out.println.
The difference lies in how they handle the cursor: System.out.println moves the cursor to a new line after the information has been displayed, while System.out.print does not.
- Remember to add a space after "hello" to produce the correct output.
- A statement always ends with a semicolon (
;). - Observe the indentation applied to the second line. It is conventional to apply indentation to statements within a block of code. A code block is anything within a set of curly braces (
{}). - The compiler does not care if you indent or not; indentation is applied so that humans can understand the code better. Advanced IDEs, automatically indent the code for us.