Main Method in Java
The main method is the point where any java program starts executing. From here every class and function is accessed by the programmer. If there will be no main method in the program then that java program will be compiled successfully but when it will be executed by JVM during run it will generate a runtime error. Any class which has the main method can be used to run the program. There cannot be more than one main method in the entire life cycle of the program. The main methods are verified by JVM through the following keywords.
- public: It is the access modifier. It find use so that the method can access from outside of the class.
- static: It is for the method that can access without creating the object of the class.
- void: It is for the method to indicate to the JVM that the method is not returning any type of value.
- main: It is the name of the method.
- String: It is a class in java. It is used to put together strings of words rather than an array of characters.
- []args: It creates the array of String type. Name of the variable can change from args to any other thing. These generally use to access the command line arguments given during the time of executing the program through the console.
If any of the following keywords are missing then JVM will give an error as
Error:
Main method not found in class Hello, please define the main method as: public static void main(String[] args)
While defining the prototype of the main method 4 possible changes are allowed i.e. interchanging of “public with static” and “[] with args“. Proper valid declarations for the main method is:
public static void main(String []args) //valid static public void main(String []args) //valid public static void main(String args[]) //valid static public void main(String args[]) //valid public static void main(String []str) //valid
Instead of passing the argument as the array of Strings we can also provide the var-arg parameter.
public static void main(String... args)
The main methods can also be declared along with following modifiers:
-
final:
final static public void main(String args[])
- strictfp:
static strictfp public void main(String args[])
- synchronized:
static synchronized public void main(String args[])
The concept of Inheritance and the main method
When the class is inherited by other class then there are multiple cases which can occur.
Case 1:
class School { public static void main(String []args) { System.out.println("This is School class"); } } class Student extends School { public static void main(String []args) { System.out.println("This is Student class"); } }
When this program is compiled then there are 2 .class files which are generated. Whichever .class file will be executed, the output will come respectively. This is same as a comparison to method overriding but it is actually a method hiding.
Inputs in the console as
javac School.java java School java Student
Output:
This is School class This is Student class
Case 2:
If the main method is overloaded then the JVM will execute the main method which has the correct prototype of the main-method. For example,
class Hello { public static void main(String []args) { System.out.println("This is correct prototype"); } public static void main(int []a) { System.out.println("This is overloaded method"); } }
Output:
This is correct prototype This is overloaded method
This is all about the mail method in Java.