Skip to content

Latest commit

 

History

History
79 lines (48 loc) · 1.95 KB

1.1_version_enhancement_for_main_method.md

File metadata and controls

79 lines (48 loc) · 1.95 KB

Version enhancements wrt main() method:

case 1:

class Test{

}

Until 1.6 version: javac Test.java ==> works fine java Test: it results in runtime exception i.e., RE:NoSuchMethodError:main

1.7 and later:

javac Test.java ==> works fine java Test: it results in runtime exception i.e Error:Main method not found in class Test. please define main method as public static void main(String[] args) So post 1.7 more meaningful information we get.


case 2a:

public class App {

static {
	
	System.out.println("static block");
            System.exit(0);
}

}

1.7 or earlier output: static block

1.8 or later: it will directly complain about main method

Error: Main method not found in class App, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application


case 3:

public class App {

static {
	
	System.out.println("static block");
            System.exit(0);
}
  
   public static void main(String[] args){
   System.out.println("main method");

}

1.7 or earlier output: static block main method

1.8 or later: it will directly look first about main method but there is no difference in sequence: output: static block main method


Case 4:

Without writing main method can we print something to the console.

Yes we can do it by using static block. But this rule is applicable only until version 1.6. From 1.7 version main method is mandatory to print some statements to the console.