Static Block
Static block in java is executed before main method. If we declare a Static block in java class it is executed when class loads. This is initialize with the static variables. Static block in java is executed every time when a class loads. This is also known as Static initialization block. Static block in java initializes when class load into memory .
Initialization can be anything; it can be variable initialization or anything else which should be shared by all objects of that class. Static block is a normal block of code enclosed in braces { } and is preceded by static keyword.
Sample example
For writing log which class is getting called
Load details from properties file, like email address,
Reading database details
Take a hypothetical example. You have developed a logging component that
uses toString() method of the object to be logged. In case you wanted
to give an option to skip logging for a particular class, you could
create an interface (e.g. NotLoggable) which doesn't need to have any
method in it. In your logging component, you could do a check like the
following:
Static block in java is executed before main method. If we declare a Static block in java class it is executed when class loads. This is initialize with the static variables. Static block in java is executed every time when a class loads. This is also known as Static initialization block. Static block in java initializes when class load into memory .
Initialization can be anything; it can be variable initialization or anything else which should be shared by all objects of that class. Static block is a normal block of code enclosed in braces { } and is preceded by static keyword.
Sample example
For writing log which class is getting called
Load details from properties file, like email address,
Reading database details
0 comments: