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

October 2025

S M T W T F S
    1 23 4
5 678 9 1011
12 13 1415161718
19202122232425
262728293031 

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Oct. 16th, 2025 01:01 am
Powered by Dreamwidth Studios