Jun. 16th, 2005

juan_gandhi: (Default)
I have to apologize for skipping the puzzlers that I forgot... now, here's the best one.

/* source: "Java Puzzlers" by Josh Bloch and Neal Gafter*/

public class Puzzler8 {
   // incredibly lazy initialization
   public static int importantValue = 0;
   private static boolean isInit = false;

   static void init() {

      importantValue = 21;

      Thread t = new Thread(
         new Runnable() {
            public void run() {
               importantValue = 42;
               isInit = true;
            }
         });
      t.start();
      try {
         t.join();
      } catch (InterruptedException e) {
         Thread.currentThread().interrupt();
      }
   }

   static {
      init();
   }

   public int getValue() {
      return importantValue;
   }

   public static void main(String[] args) {
      System.out.println("Of all numbers, the most important for us is " + new Puzzler8().getValue());
   }
}



What will the program print:

a) "Of all numbers, the most important for us is 0"
b) "Of all numbers, the most important for us is 21"
c) "Of all numbers, the most important for us is 42"
d) Exception is thrown
e) None of the above

Enjoy...
juan_gandhi: (Default)
To my confusion, I've just discovered that interface and functionality there are much better.

So... I don't know... do these two syndicate? Is there a way to migrate, with all the crap?

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

September 2025

S M T W T F S
 1 2345 6
78 9 10 111213
14 151617 181920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 19th, 2025 06:05 pm
Powered by Dreamwidth Studios