we can provide any initialization inside static block and also inside constructors . can anybody provide a real life example when a static block initialization is a must , not constructor ? Regards, Ayan
When you want to initialize static data -- data that is shared by a whole class. Code in a constructor will execute every time an object is created, which is generally not what you want for static data.
Static blocks are comparable with method blocks where you can put some code stuff in it, with the difference that they're executed only once per runtime, before the instance initialization blocks and the constructor. The initialization blocks and the constructor are executed everytime when an instance is created.