The main method is the entry point of any core Java program. Here, I mention the core Java program specifically because, in all the other java programs like Servlets, applets and any Java-based frameworks, they have their own life cycles and own entry points.
For example in Servlet programming, we don’t have any main method, but it does not mean that there is no entry point for a servlet. The starting point of a servlet is init()
.
public static void main(string[] args)
Java main method is called by the main Thread, which is created by the Java Virtual Machine(JVM). And the program containing the main method runs until the main thread is alive. After completion of the main thread, the program will be terminated.
java class name
When we hit the above (class name followed by the “java”) command, the JVM checks for the main method with the default prototype.
Default Syntax for main method:
public static void main(String args[])
Why the main method is public?
The main method should be public. The public keyword is the access modifier in java. By making the main method as public, the method can get access from anywhere, any package. For instance, consider in your system JVM is installed under the “C:\” directory but the java program containing the main method is in the “D:\” directory, by making the main method as public the JVM which is present in “C:\” will be allowed to get access to the main method in “D:\”.
Why the main method is static?
Static is also one of the access modifiers in Java. When we declare a variable, method, block, class with static modifier those are all under class level access, and not for a specific instance or object. Hence the static functionalities are directly accessed by the class name. For direct accessing with JVM, the main method should be static. Hence JVM will call the main method directly by using the class only, without creating any instance of the main method class.
Why the main method doesn’t return any value?
Whenever we declare a method with “int” or any return type except “void”, that means we can catch hold of that particular return value and make use of that value from the called method and do something on that value. But the main method is called by the JVM. JVM will not expect any value from the main method as a return value because there is no more functionality done by the JVM after getting the return value from the main, hence the main method is declared as “void”.
Why the main method name declared as “main”?
In JVM the main method name is configured as a main. It is not necessary to keep the main method name always as main. If we change the configuration of JVM with respect to the main method, then we can change the name of the main method as we want.
What is String[] args?
Java main method takes the array of Strings as a parameter. By using this parameter we can pass the command line arguments to the main.
The possible customizations on the main method
- public static void main(String args[])
- public static void main(String[] args)
- public static void main(String []args)
- public static void main(String… args)
- static public void main(String args[])
- public static void main(int[] args)
Do you know?
- We can make the main method as final.
- We can make the main method synchronize.
- We can make the main method as strictfp (strict floating point).
- We can overload the main method.
- We can also override the main method but it is termed as data hiding (not overriding).
Hence the below syntax is applicable for the main method:
final static synchronize strictfp public void main(String args[]){}
Overloading the main method
We can overload the main method, and also we can call the main method if need.
Example:
public class Parent {
public static void main(String[] args) {
System.out.println("Sring args::");
}
/**
* Overloading main method
*/
public static void main(int[] args) {
System.out.println("int args::" + args[0] + "" + args[1] + "" + args[2]);
}
}
class Child {
public static void main(String[] args) {
int[] intArgs = {1, 2, 3};
Parent.main(intArgs);
}
}
By running the Child class we get the below output.
Output:
int args::123
Method hiding
If the main method is overridden it is termed as Method Hiding. We can hide the main method as shown below.
Example:
public class Parent {
public static void main(String[] args) {
System.out.println("In Parent main");
}
}
class Child extends Parent {
public static void main(String[] args) {
System.out.println("In Child main");
}
}
By running the Child class we get the below output.
Output:
In Child main
Happy Learning 🙂