java - Use of static init block -


I know how a static init block works.
Can anyone tell me some common uses of this?

When you want to start one or more static variables in one place

< P> This is useful, because you can apply exception handling, which is not possible with in-line initialization.

For example:

  Public Static Image Default Icon = ImageIO.read (..);  

can be started with

  public static imaging default Inc.; Static {try {defaultIcon = ImageIO.read (..); } Hold (IOException pre) {System.out.println ("no default icon is available"); }}  

Another application is a complex preliminary For example, if more than one line of code is needed to start an item, assume that you have a configuration:

  Public Static Configuration Configuration; Stable {confuguration = new configuration (); Configuration.setSomething (..); Configuration.setSomethingElse (..); ...}  

The third use is to start some external API infrastructure. An example from my current project:

  Fixed {org.apache.xml .security.init.init (); }  

But, as the Mikola Roundabayev pointed out, static initialization makes the block code less readable, so use them with caution, the same thing is more transparent in a steady manner.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -