Advertisement
Guest User

for stackoverflow.com/questions/70689

a guest
Sep 9th, 2015
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. class Bar {
  2.     private static class BarHolder {
  3.         static {
  4.             System.err.println("BarHolder initialization");
  5.         }
  6.         public static Bar bar = new Bar();
  7.     }
  8.  
  9.     public static Bar getBar() {
  10.         return BarHolder.bar;
  11.     }
  12.  
  13.  
  14.     public Bar() {
  15.         System.err.println("complex Bar ctor running");
  16.     }
  17.  
  18.  
  19.     public static void main (String[] args) {
  20.         System.err.println("fetching a Bar");
  21.         Bar some_bar = Bar.getBar();
  22.         System.err.println("fetching another Bar");
  23.         Bar another_bar = Bar.getBar();
  24.         System.err.println("done");
  25.     }
  26. }
  27.  
  28. /*
  29. Running with "java Bar" :
  30. fetching a Bar
  31. BarHolder initialization
  32. complex Bar ctor running
  33. fetching another Bar
  34. done
  35.  
  36.  
  37. Running with "java -verbose:class Bar" :
  38. [...several hundred lines of JRE loading...]
  39. fetching a Bar
  40. [Loaded Bar$BarHolder from file:/C:/src/]  <-- delayed loading
  41. BarHolder initialization
  42. complex Bar ctor running
  43. fetching another Bar
  44. done
  45. [Loaded java.lang.Shutdown from C:\....\rt.jar]
  46. [Loaded java.lang.Shutdown$Lock from C:\....\rt.jar]
  47.  
  48. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement