main method responsibilities in java

Every Java class is contains a main() method or not and whether  the main() method is declared properly or not. These checkings are not responsibilities of compiler at runtime,JVM is responsible for these checking . If the JVM unable to find required main(0 then we will get runtime Exception saying NoSuchMethodError: main



Ex:
Class MainTest
{
}
Compile: javac MainTest.java
Run       : java MainTest
main method


  • JVM always search for the main() with following signature

Public static void main(Strign[] args)

  • If we are performing any change to the above signature we will get runtime Exception saying “NoSuchMethodError:main . Any way the following changes are acceptable.
  • We can change the order of modifiers that is instead of public static we can take static public. We can declare String in any valid form.




String[] args
String  []args
String args[]

  • Instead of args we can take any valid java identifier.
  • Instead of String[] we can take var-arg String parameter is String…

Main(String[]  args) main(String… args)

  •  Main() can be declared with the following modifiers also

Final
Synchronized
Strictfp
Ex:
Class MainTest1
{
final static strictfp synchronized public void main(Strign… args)
{
System.out.println(“hi User….”);
}
}
Some question related to main method
Q) Which of the following main() declarations are valid?
Ans).
Public static int main(String[] args)
Static public void Main(String[]  args)
Public synchronized strictfp final void main(String args[])
Public final static void main(String… args)
Public strictfp synchronized static void main(String args)

Q) In Which of the above cases we will get complitime error
Ans) No where,all cases we will Compile
Inheritance concept is applicable for static method including main() also.Here if the child class doesn’t contain main() then parent class main() will be executed

while executing child class
EX:
Class MainTest2
{
Public static void main(String[] args)
{
System.out.println(“hello user”);
}
}
Class MainChild extends MainTest2
{
}
Compile: javac MainTest2.java
Run    : java MainTest2
OutPut : Hello user

Run    : java MainChild
OutPut: Hello user

Ex2:
Class MainParent
{
Public static void main(String[] args)
{
System.out.println(“hello hi user I am parent class”);
}
}
Class MainChild extends MainParent
{
Public static void main(String args[])
{
System.out.println(“hello hi user I am child class”);
}
}
Compile: javac MainParent.java
Run    : java MainParent
OutPut : hello hi user I am parent class

Run    : java MainChild
OutPut : hello hi user I am child class
It seems to be overriding concept is applicable for static methods,but it’s not overriding but it is MethodHidding.Overloading concept is applicable for main() but JVM

always calls String[] args method only . the other method we have to call explicitly.
Ex:
Class Test
{
Public static void main(String args[])
{
System.out.println(“hello user”);
}
Public static void main(int args[])
{
System.out.println(“hello user 2”);
}
}
OutPut: hello user

Q).Instead of main() is it possible to configure any other method as main()?
Ans).
Yes,but inside JVM we have to configure some changes then it is possible.