Powered by Blogger.

Tuesday, 8 April 2014

Static Block

By Shambhu Kumar  |  03:45 No comments

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

Marker interface is an interface that doesn't have any methods in it. It is generally used to identify classes for some type of processing. 

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:
if(!(obj instanceof NotLoggable)) log(obj.toString());

I hope you get the idea how marker interfaces can be used. 




Author: Shambhu Kumar

Hello, I am Author, decode to know more: In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet.


Recent Articles

© 2014 Learning Java. WP themonic converted by Bloggertheme9. Published By Gooyaabi Templates