The main() method of Java is one of the first things to learn when coding in Java. The main() method is an important building block of Java programming. The sequence of the keywords and the main() method is an important aspect since the Java virtual Machine will first pick the main method and execute the rest afterward. Therefore the keywords are declared according to the sequence.
In this write-up, the main() method of Java will be demonstrated.
Working of the main() Method of Java
The main() method of Java can be placed in any declared class of Java and it can also contain code to call other methods in the program. The main() method comprises keywords. The keywords of the main() method are important and each keyword is declared in its place according to the defined sequence.
Let us have a look at the syntax of the main() method

The basic implementation of the Main method of Java is depicted below which prints the declared string.
//Declare a class for String contains() method
public class MainJavaMethod{
//Main class of Java
public static void main(String[] args) {
//Declare a string
String str1 = "Java Programming needs practice";
System.out.println(str1);
}
}
Output
The output below prints the declared String.

Now we will look at the individual parts of the Main method in Java. The first one is public.
Access Specifier – “Public”
The first part of the main() method is an access modifier that specifies who can access the method and from where it can be accessed. It is always declared as public because the Java Virtual Machine calls it from outside the declared class.
//Declare a class for String contains() method
public class MainJavaMethod{
//Main class of Java
private static void main(String[] args) {
//Declare a string
String str1 = "Java Programming needs practice";
System.out.println(str1);
}
}
In the above block of code, when “private” is declared instead of “public” it throws an error.
Output
The below output prints an error when the public is not declared in the main() method of Java.

Keyword – “static”
The static method is applied using the keyword “static”. It is useful since there is no need to create an object. Therefore, static is used in the main() method since it is invoked without creating an object. Code below depicts the working.
//Declare a class for String contains() method
public class MainJavaMethod{
//Main class of Java
public void main(String[] args) {
//Declare a string
String str1 = "Java Programming needs practice";
System.out.println(str1);
}
}
In the above code when static is not declared, an error message is returned.
Output
The output below depicts that the static method is required in the main() method of Java.

Return Type – “void”
The method that does not return anything is referred to as the “void method” in Java. It is used with the main() method as the stated method doesn’t retrieve anything. There is no need to return a value since the main() method terminates when the Java program ends.
//Declare a class for String contains() method
public class MainJavaMethod{
//Main class of Java
public static main(String[] args) {
//Declare a string
String str1 = "Java Programming needs practice";
System.out.println(str1);
}
}
In the above code, the void keyword is missing to check whether a different output occurs or not.
Output
The below output depicts that the void return type is important to declare.

Method – “main()”
This is a method that a Java virtual Machine actually looks for when it starts the program.
//Declare a class for String contains() method
public class MainJavaMethod{
//Main class of Java
public static void hmain(String[] args) {
//Declare a string
String str1 = "Java Programming needs practice";
System.out.println(str1);
}
}
In the above code, the “hmain()” is declared instead of the main() method. The output will result in an error.
Output
The output below depicts that the declaration of the main() method is important.

Arguments – “String[ ] args”
This uses a string array and each string present in the array represents a command line argument. The command line argument is basically used to pass any information at runtime. The code block implements the String[ ] args of the main() method.
//Declare the Class
class StringMethod {
//The main() method of Java
public static void main(String[] args) {
for (String a : args)
System.out.println(a);
}
}
In the code above:
- The class is declared as “StringMethod” to elaborate the working of the String[]args.
- The string “a” is declared for command in line arguments.
- The println() method of Java prints the output.
When the above block of code is executed, the following code is to be executed in the terminal window as below:
javac app.java
java StringMethod 1 2 3 4 5 "Testing the String[] args of main() Method"
When the code is executed the output now appears to be which consist of javac app.java where app is the file name where the code is executed. The class name is StringMethod which consists of strings.

The working of each keyword and the main() method is completed.
Conclusion
The main() method of Java consists of the code to be executed by the declared class. It is considered the most basic feature of Java programming. In this write-up, we have effectively demonstrated the working of the main() method of Java.