java class without any method, but contains logic under static keyword -


i have few questions below code snippet:

public class configuration {     public static string temp_dir;     public static list<string> levents;     ......//some more public static members......     static{       //logic fill members of class     } } 
  1. i wondering when logic fills members executed?
  2. and approach different if have used actual static method execute logic , call once?

the

static {     //logic fill members of class }  

code known static initializer.

i wondering when logic fills members executed?

the java language specification says

a static initializer declared in class executed when class initialized (§12.4.2).

you ask

and approach different if have used actual static method execute logic , call once?

with static initializer, jvm takes care of executing code. static method, have call yourself. equivalent, want guarantee method called/executed once.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -