Recent Posts

Tuesday, 24 May 2016

About Main Method


     Whether the class contains main() or not and whether the main() is properly declared or not. These changes are not responsible of compiler. At runtime, JVM is responsible for these checking. If JVM unable to find required main() then we will get runtime exception saying NoSuchMethodError:main. 
Eg:
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
E.g:
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.
class P {
public static void main(String[] args) {
System.out.println("This is Ashok");
}
}
class C extends P {
}

javac P.java
java P
O/P Hi this is Ashok
java C
O/P Hi this is Ashok
class P {
public static void main(String[] args) {
System.out.println("This is Ashok");
}
}
class C extends P {
public static void main(String[] args) {
System.out.println("This is Vinod");
}
}

javac P.java
java P
O/P This is Ashok
java C
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.
E.g:
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");
}
}
O/P 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.

Next Tutorial  Command Line Arguments

Previous Tutorial  Var Arg Methods

5 comments:

  1. Hi Sir,

    You had explained very well about the main method significance and usage. Could you please explain, What kind of datatype arguments are accepted for String[] args when running through command line.

    Thank you.

    Regards,
    Srikanth.K

    ReplyDelete
    Replies
    1. Thank you @Srikanth Jogeswar.. In java only String arguments are allowed.. If you are trying to give integer arguments those are converted into strings data types only for example
      package com.ashok.test;

      public class MainTest {

      public static void main(String[] args) {
      int sum = 0;
      for (int i = 0; i < args.length; i++) {
      System.out.println(args[i]);
      sum = sum + Integer.parseInt(args[i]);
      }
      System.out.println("The sum of the arguments passed is " + sum);
      }
      }
      If you are passing arguments as "5" "6" {String args} then output is
      5
      6
      The sum of the arguments passed is 11

      If you are passing arguments as 5 6 {int args} then output is
      5
      6
      The sum of the arguments passed is 11




      Delete
    2. Thank you for being patient with me. One last question is, are boolean datatype arguments are accepted as command line argument.

      Delete
    3. Thank you.. In Java any type of arguments it is converted in to String only.. For example
      package com.ashok.test;

      public class MainTest {

      public static void main(String[] args) {
      for (int i = 0; i < args.length; i++) {
      System.out.println(args[i]);
      }
      }
      }
      If you are passing arguments as true false {boolean args} then output is
      true
      false

      Delete