what is different between static block and public static void main(String a[]) method,we execute method without main method(by static block) why need of public static void main(String [])?
With out main() and static block also run our java program it is possible through static obj creation class Nomainnostatic { Nomainnostatic() { System.out.println("vaaniMohan"); System.out.println("hai"); System.exit(1); } static Nomainnostatic n=new Nomainnostatic(); }; Support some jdk versions only Exp:when ever static obj is created the default constructor will be called,so the constructor block will be executed. RammohanReddy
Static blocks are meant to be run once the corresponding class is loaded. The main() method, however, is the entry point to your program and it can be invoked multiple times.
static (block) executes once for every instance of the class (per JVM) - it allows you to initialise some values per class instance, it does NOT replace main() method for various reasons, it coexists with main() method which is static too. One of the main reasons for having main() method is that you can actually pass parameters to it, unlike static block.
Ads