What is the Java main() Method?
The java main() method is the initial point of Java Virtual Machine (JVM). It is used to initiate the execution of a Java program. The main() method would probably be the first method you’ll learn when you start Java programming as it’s the essential part of the execution of any Java program. The general syntax of the main method is as follows.
public static void main(String[] args){
// some code here in the main() method
}
Example
class NewYear {
public static void main(String... newyearargs) // main() method
{
System.out.println("Happy New Year 2023");
}
}
P. S. You can change the “args” part of the main method syntax and name it on your own as shown in the example.
There’s also an alternative way to write the “String[]” part of the main method as “String… args”.
The main() Method Modifiers
The signature method for writing the main function consists of three main modifiers:public
This modifier is popular with the name of the access specifier. As the name defines, this modifier grants access to the Java Virtual Machine (JVM) for the execution of the program. To grant access to JVM, one should use public while writing the code to allow Java Runtime Environment (JRE) to access and execute the method.Example
public class NewYear{
//using main function
static void main(String... newyearargs){ // defining main() method here
System.out.println("Happy New Year");
}
}
The following error happens when you compile and run the program because the main method isn't public and the JRE can't find it.
Error: Main method not found in class NewYear, please define the `main` method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
static
There is no object of the class existent when the Java program starts. The main method must be static in order for the JVM to load the class into memory and then call the main function without first generating an instance of the class.
public class NewYear{
public void main(String... newyearargs){ // defining main() method here
System.out.println("Happy New Year");
}
}
The following error happens when you compile and run the program because the main method isn't static.
Output
Error: Main method is not static in class NewYear, please define the `main` method as:
public static void main(String[] args)
void
The return type must be provided by every Java function. When it returns nothing, the Java main function return type is void. The Java application finishes when the main method completes, therefore there is no need for a returned object. The main function in the following example code attempts to return something when the return type is void:
public class NewYear{
public static void main(String... newyearargs){ // defining main() method here
return 0;
}
}
The following error happens when you compile and run the program because the main method is void and does not return anything.
Output
NewYear.java:5: error: incompatible types: unexpected return value
return 0;
^
1 error
main
It is a predefined default signature in the JVM. JVM calls this method to execute a program line by line and ends the execution after this method is completed. We can also call the main() method multiple times. Following is the illustration of the main function:
public class NewYear{
public static void newMain(String... newyearargs){
System.out.println("Happy New Year");
}
}
The following error happens when you compile and run the program because the main method isn't named correctly.
Output
Error: Main method not found in class NewYear, please define the `main` method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
String[] args
The main function in Java takes a single String array as input. The array's strings are command line arguments. At runtime, users can use command line arguments to influence the program's operation or to send data to the program. The following example shows an illustration to print the command line arguments.
public class NewYear{
public static void main(String... newyearargs){ // main() function goes here
for(String s : newyearargs){
System.out.println(s);
}
}
}
Execute the program with few arguments and you'll see the arguments will be printed in the console when you compile the program.
Output
2 0 2 3
Happy New Year
GO TO FULL VERSION