JavaIntellectualTricks

Exploring main( ) method

Whether class contains main( ) method or not and whether main() is declared according to requirement or not , these things won't be checked by compiler. At run time JVM is responsible to check these things. If JVM unable to find main(), then we will get run time exception saying ”NoSuchMethodError: main”

class Test{
}
>javac Test.java // No error
>java Test // NoSuchMethodError: main

At run time JVM always searches for the main() with the following prototype.
public static void main(String[] args)
Here:
public refers – to call by JVM from anywhere
static refers – without existing object also JVM has to call this method for one time.
void refers – main() won't return anything to JVM
main refers – this is the name which is configured inside JVM
String[] args refers – command line arguments

The above syntax is very strict and we perform any change then we will get run time exception saying NoSuchMethodError: main

Even though the above syntax is very strict, the following changes are acceptable.

  • Instead of public static , we can take static public i.e the order of modifier is not important.             We can declare String array in any acceptable form

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

  • We can take any valid java identifier instead  of args.
main(String[]  purna)

  • We can replace String array with var-arg parameter

main(String...  args)

  • We can declare main() with the following modifiers

final, synchronized, strictfp
class Test{
static final synchronized strictfp public void main(String...  purna){
System.out.println(“valid main method”);
}
}

Q1. Which of the following main() declarations are valid?
1. public static void main(String args) // (Invalid)
2. public static void Main(String[] args) // (Invalid)
3. public  void main(String[] args) // (Invalid)
4. public static int  main(String[] args) // (Invalid)
5. final synchronized strictfp public void main(String[]  args) // (Invalid)
6. final synchronized strictfp public  static void main(String[]  args) // (valid)
7. public static void main(String...  args) // (valid)

Q2.In which of the above cases we will get compile time error ?
We won't get compile time error anywhere. But except last two cases in remaining we will get runtime exception saying  NoSuchMethodError: main

Case 1: 
Overloading of the main method is possible ,but JVM will always call String[] arguments main() only.
The other overloaded method, we have to call explicitly like normal method call.
class Test{
public static void main(String[] args){
System.out.println(“String []”);
}
public static void main(int[] args){ //overloaded methods
System.out.println(“int []”);
}
}
o/p: String [ ]

Case 2:
inheritance concept applicable for main(), hence while executing child class if child does not contain main(), then parent class main () will be executed.

//p.java
class P{
public static void main(String[] args){
System.out.println(“Parent main”);
}
}
class C extends P{
}
>javac P.java
>java P  // o/p: Parent main
>java C // o/p: Parent main

Case 3: 
It seems overriding concept applicable for main(), but it is not method overriding, but it is method hiding.
//p.java
class P{
public static void main(String[] args){
System.out.println(“Parent main”);
}
}
class C extends P{
public static void main(String[] args){ // It is method hiding but not method overriding
System.out.println(“Child main”);
}
}
>javac P.java
>java P  // o/p: Parent main
>java C // o/p: Child main

Note:
For main() inheritance and overloading concepts are applicable, but overriding concept is not applicable. Instead of overriding,  method hiding is applicable.

1.7 version enhancements w.r.t main():

Until 1.6 version, if the class doesn't contain main(), then we ll get run time exception saying  NoSuchMethodError: main. But from 1.7 version onwards instead of   “NoSuchMethodError: main” we will get more elaborated information.
Example:
class Test{
}
1.6 version:
>javac Test.java
>java Test //R.E: NoSuchMethodError: main
1.7 version:
>javac Test.java
>java Test   //R.E. Main method not found in class Test, please define the main method as:
public static void main(String[] args)

From 1.7 on wards main() is mandatory to start program execution,hence even though class contains static block it won't be executed if the class does not main().
Example 1:
class Test{
static {
System.out.println(“static block”);
}
}
1.6 version:
>javac Test.java
>java Test // static block & R.E: NoSuchMethodError: main
1.7 version:
>javac Test.java
>java Test //R.E. Main method not found in class Test, please define the main method as:
public static void main(String[] args)
Example 2:
class Test{
static {
System.out.println(“static block”);
System.exit(0);
}
}
1.6 version:
>javac Test.java
>java Test // static block
1.7 version:
>javac Test.java
>java Test //R.E. Main method not found in class Test, please define the main method as:
public static void main(String[] args)

Example 3:
class Test{
static {
System.out.println(“static block”);
}
public static void main(String[] args){
System.out.println(“main method”);
}
}
1.6 version:
>javac Test.java
>java Test // static block
    // main method
1.7 version:
>javac Test.java
>java Test // static block
    // main method

1.6 flowchart:
1. Identification of static members
2. execute static blocks and static variable assignments
3. check for main()

  • If it is available execute main()
  • If it is not available R.E: NoSuchMethodError: main
1.7 flowchart:
1. check for main()

  • If it is available identifies static members then execute static blocks and static variable assignments
  • If it is not available Error: R.E. Main method not found in class Test, please define the main method as: public static void main(String[] args) then program terminates.
Q. Without writing main() is it possible to print some statements to the console?
Ans: Yes by using static block, but this rule is applicable until 1.6 versions, but from 1.7 version onwards it is impossible to print some statements to the console with out writing main().



No comments:

Post a Comment