Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Interview-Questions JAVA what is different between static block and public static void main(String a[]) method
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


amit kumar saini
JAVA what is different between static block and public static void main(String a[]) method
2 Answer(s)      2 years and 6 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 [])?


Advertisement
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.



Related Tutorials/Questions & Answers:
 

 

 

DMCA.com