The main()
method in Java is the entry point for any Java application. It is executed by the Java Virtual Machine (JVM) when the program is run.
Let us analyze the syntax in detail and examine the purpose of each individual component.
public: public
is an access modifier. Access modifiers in Java specify the accessibility or scope of a method. The access level of a public
modifier is everywhere. It can be accessed from within the class, outside the class, within the package, and outside the package. We make the main()
method public
so the JVM can access the method from anywhere.
static: The static
keyword means the main()
method belongs to the class itself, not to any object. So, you don’t need to create an object of the class to run the main()
method.
void: void
means the main()
method doesn’t return any value. It's just used to start the program, so it doesn’t need to send anything back. In Java, every method must mention what it returns, and void
means 'nothing'.
main: main
is the name of the method. It must be written exactly as main
, because the Java Virtual Machine (JVM) looks for this method to start running the program.
String[] args: String[] args
is a way to receive input from the command line when the program starts. It’s a list (or array) of text values that you can use in your program if needed.
Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
In this program, we are simply printing the data on console.
Output
Hello, World!
How to take Command Line Arguments?
public class MasteringBackend {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println("First argument: " + args[0]);
} else {
System.out.println("No arguments passed.");
}
}
}
In this example, we are taking inputs from the command line by passing the parameter at runtime using the command below.
java MasteringBackend Hello
As we passed the first argument as Hello
, we get the output as shown below.
Output
Hello
Using String… args
String[] args
and String... args
are both the same.
public class MasteringBcakend {
public static void main(String... args) {
for (String arg : args) {
System.out.println("Argument: " + arg);
}
}
}
In this example, we are taking input from the command-line arguments. When we run the command by passing all the numbers of parameters, all the values are printed to the console.
java MasteringBcakend Learn Java Backend
Output
Learn Java Backend
Note
The
main()
method is declared aspublic
so that the JVM can access it from outside the class. It is marked asstatic
so it can run without creating an object of the class. If it is notpublic
, the program will not start.
System.out.println()
System.out.println()
is a statement that is used to print data to the console.
System:
System
is a special built-in class in Java that allows interaction with the underlying system, such as printing messages to the screen or reading input from the keyboard. It's part of Java's core library (java.lang
package).
out: out
is an object that is part of the System
class. It acts as a channel for sending data to the console. When you want to display information to the user, you send it through System.out.
println(): println()
is a method that prints the specified text to the console and moves the cursor to a new line. This makes it easy to display each piece of information on a separate line.
Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
In this example, we are printing the data to the console.
Output
Hello, world!