Main Method

Main Method

In this tutorial, we are going to discuss about main method in java. In Java, the main method is the entry point for a Java application. When you run a Java program, the Java Virtual Machine (JVM) starts executing the code from the main method.

Main Method

The Java compiler or JVM looks for the main method when it starts executing a Java program. The signature of the main method needs to be in a specific way for the JVM to recognize that method as its entry point. If we change the signature of the method, the program compiles but does not execute.

Whether the class contains main() or not and whether the main() is properly declared or not. These changes are not responsible of compiler. At run time, JVM is responsible for these checking. If JVM unable to find required main() then we will get run time exception saying NoSuchMethodError:main. 

E.g

class Test {

}
javac Test.java // Compile fine
java Test // NoSuchMethodError:main

JVM always searches for main() method with the following signature.

public static void main(String[] args)

Here

public --> To call by JVM from any where (Call from command line etc)
static --> Without existing object also JVM has to call this method
void --> Main method won't return anything to JVM
main --> Name of the method which is configured inside JVM
String[] args --> Command line arguments

The above syntax is very strict and if we perform any change then we will get RuntimeException saying NoSuchMethodError:main. Even though above syntax is very strict the following changes are acceptable.

1. Instead of public static we can declare static public i.e., the order of modifier is not important.

2. We can declare String[] in any acceptable form

main(String[] args)
main(String []args)
main(String args[])

3. Instead of args we can declare any Java valid identifier.

4. Instead of String[] we can take var arg String parameter

main(String... args)

5. main() can be declared with the following modifiers also

final
synchronized
strictfp
package com.ashok.test;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test {
   static final synchronized strictfp public void main(String... ashok) {
      System.out.println("Hi this is Ashok");
   }
}

Inheritance concept is applicable for static methods including main() also. Hence if the child class doesn’t contain main() then parent class main() will be executed.

package com.ashok.test;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Parent {
   public static void main(String[] args) {
      System.out.println("This is Ashok");
   }
}

class Child extends Parent {
}

Output

javac Parent.java

java Parent
O/P This is Ashok

java Child
O/P This is Ashok
package com.ashok.test;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Parent {
   public static void main(String[] args) {
      System.out.println("This is Ashok");
   }
}
class Child extends Parent {
   public static void main(String[] args) {
      System.out.println("This is Vinod");
   }
}
javac Parent.java
java Parent
O/P This is Ashok
java Child
O/P This is Vinod
  • It seems to be overriding concept is applicable for static methods, but it’s not overriding but it is method hiding.
  • Overloading concept is applicable for main() but JVM always calls String[] argument method only. The other method we have to call explicitly.
package com.ashok.test;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test {
   public static void main(String[] args) {
      System.out.println("This is Ashok");
   }
   public static void main(int[] args) {
      System.out.println("This is Vinod");
   }
}

Output

This is Ashok

Note

Inside of JVM is it possible to configure any other method as main method.?

Ans : Yes, but inside JVM we have to configure some changes then only it is possible.

That’s all about main method in Java. If you have any queries or feedback, please write us at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!

Main Method
Scroll to top