You are currently viewing The main() Method: Entry Point to Java Programs
java_logo

The main() Method: Entry Point to Java Programs

In the Java programming language, the main() method serves as the entry point for executing a Java program. This main() method is the gateway between the startup of a Java process, which is managed by the JVM, and the beginning of the programmer’s code. Understanding the structure and functionality of the main() method is crucial for developing and running Java applications. In this chapter, we will explore the purpose of the main() method, its different parts, and various ways to interact with the program.

1. Creating the main() Method

public class MyProgram {
    public static void main(String[] args) {
        // Code execution starts here
        // Rest of the program
    }
}

In the above example, we define a class named MyProgram. Inside this class, we declare the main() method. It has the following key components:

  • public: The main() method is marked as public, indicating that it can be accessed from outside the class.
  • static: The main() method is declared as static, which means it belongs to the class itself rather than an instance of the class. This allows the JVM to call the method without creating an object of the class.
  • void: The main() method has a return type of void, indicating that it doesn’t return any value.
  • String[] args: The main() method takes a parameter of type String[] named args. This parameter allows command-line arguments to be passed to the program.

2. Compiling and running the code

The simplest possible class with a main() method looks like this:

public class Hello{
    public static void main(String[] args){
        System.out.println("Hello World");
    }
}

Here’s a step-by-step explanation of how to compile this code with javac and run this code with java:

Compiling the Code with javac:
Open a terminal or command prompt.
Navigate to the directory where the Java source file is located using the cd command. For example:
cd /path/to/your/directory

Compile the Java source file using the javac command followed by the filename:
javac Hello.java
This command instructs the Java compiler (javac) to compile the source code and generate the bytecode file (Hello.class).

If the compilation is successful and there are no errors in the code, you should see a new file named Hello.class in the same directory. This file contains the compiled bytecode that can be executed by the Java Virtual Machine (JVM).

Running the Code with java:
After successful compilation, execute the Java program using the java command followed by the class name (without the .class extension). For example:
java Hello
This command instructs the JVM to run the compiled Java program.

The program will execute, and you will see the output displayed in the terminal or command prompt based on the logic defined in the main() method. In this case the output will be “Hello World”

3. Command-Line Arguments

The args parameter in the main() method allows you to pass command-line arguments to your program when it is executed. These arguments can be used to customize the behavior of the program at runtime. Here’s an example:

public class CommandLineArgsExample {
    public static void main(String[] args) {
        // Accessing command-line arguments
        System.out.println("Number of arguments: " + args.length);
        for (String arg : args) {
            System.out.println("Argument: " + arg);
        }
    }

In this example, the main() method retrieves the command-line arguments passed to the program. The args parameter is an array of strings, where each element represents an argument. By accessing args.length, you can determine the number of arguments passed. The for loop then iterates through each argument, printing its value.

an example:

compile the code:
javac CommandLineArgsExample.java

run the code with 3 parameters:
java CommandLineArgsExample apple banana cherry

output:
Number of arguments: 3
Argument: apple
Argument: banana
Argument: cherry