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 } }
- i wondering when logic fills members executed?
- 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
Post a Comment