Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training Big Data and Hadoop Training Java Training  
 

JAVA what is different between static block and public static void main(String a[]) method

Ads

 
 

Share on Google+Share on Google+

amit kumar saini
JAVA what is different between static block and public static void main(String a[]) method
2 Answer(s)      5 years and 2 months ago
Posted in : Java Interview Questions

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 [])?

Ads
View Answers

November 1, 2012 at 1:26 PM


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



June 5, 2012 at 3:38 PM


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