Main method
previous NextThe main() method is the starting point for JVM to start execution of a Java program. without main method we can compile Java Programm but we can not exicut. If JVM unable to find the required main() method then we will get runtime exception saying NoSuchMethodError: main
the Syntex of required main method is
class Test{
public static void main(String[] args) {// required main() for JVM
}
}
Explainatin of main method
public : public is a access modifier which is used for calling from anywhere. in main method we use public because JVM can call main method from anywhere and any directory.
static : static is a keyword which is used for invoking method without object if are using static keyword in our method then no need to make object for calling that method we can call that without object thats why main method shuld be static
void : void is a return type keyword which is used when the method doesn't return anything. In the main method no need to return anything JVM don't want anything thats why main method's shuld not be return anything and we use void
main : main is a identifier or name of the method . JVM always call that method whose name is main
String [] args : String is a name of class which work as a data type & we can say that its a data type also. and in main method we are using "String args []" to take values from the command line argument , We can use any identifier instead args.
what happens if we are not used main method ?
if we will not use main method in our program then there we will not get any error at compile time but we will get run time exception as follows
class Test{
public static void main(String[] args) {// required main() for JVM
}
}
output
Error: Main method not found in class vivek, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
Note : we can use var-arg method in main() method's parameter
For Example
class Test{
public static void main(String... args) {
}
}

Overloading of main method
overloading of main method is possible but JVM always call public static void main(String args[]) only.
class Test{
public static void main(String [] args){
System.out.println("JVM method");
}
public void main(String args){
System.out.println("my method");
}
}
output
JVM method