Why should I use static initialization block when I can initialize static members through a constructor?
Firstly, you might never have any instances of your class. Or you might want to have the static members iniailized before you have any instances of the class.
Secondly, initializing static members from constructors is more work:
Lastly, it's usually the wrong thing to do conceptually (I say "usually" because there are legitimate uses for lazy initialization).
A static member is not associated to any instance of the class, while the constructor creates an instance. You may use static members without having a single instance of the class, they will still have to be initialized. In this case a constructor can not do the job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With