1) Why static block?
Java static blocks are used to initialize the static variables. The static block code is loaded when Java Virtual Machine(JVM) loads the class. Since we know, static variables can not be included in constructor, only member variables can be initialized in constructor. So, for static variables, the static blocks work like a constructor. We can have any number of static blocks that are executed at the time class is loaded. See the example below:
class MyClass { Java static blocks are used to initialize the static variables. The static block code is loaded when Java Virtual Machine(JVM) loads the class. Since we know, static variables can not be included in constructor, only member variables can be initialized in constructor. So, for static variables, the static blocks work like a constructor. We can have any number of static blocks that are executed at the time class is loaded. See the example below:
static Map
(TO be continued...)
No comments:
Post a Comment