8

I originally thought that static blocks were for static variables but the compiler allows both A and B to compile and run, what gives?
A

   private static final Map<String,String> m = new HashMap<String,String>();

        {
            m.put("why", "does");
            m.put("this","work");
        }

B

 private static final Map<String,String> m = new HashMap<String,String>();

        static{
               m.put("why", "does");
               m.put("this","work");
             }

Running System.out.println(Main.m.toString()); for A prints

{}

but running the same for B prints out in Yoda-speak

{this=work, why=does}

1
  • Is there an alternative to static and non-static blocks? Commented Aug 7, 2013 at 6:43

2 Answers 2

13

The non static block is executed when an "instance" of the class is created.

Thus

System.out.println(Main.m.toString());

prints nothing because you haven't created an instance.

Try creating an instance first

 Main main = new Main();

and you'll see the same message as B

As you know class variables (declared using static) are in scope when using instance blocks.

See also:

Anonymous Code Blocks In Java

Sign up to request clarification or add additional context in comments.

1 Comment

Which makes perfect logical sense
6

In A, you have an instance initializer. It will be executed each time you construct a new instance of A.

If multiple threads are constructing A instances, this code would break. And even in a single thread, you normally don't want a single instance to modify state that is shared by every instance. But if you did, this is one way to achieve it.

1 Comment

Erickson is correct - create a new instance, and they will be equivalent. And if you change the values for those keys around, and then create another new instance, they will be replaced with the original values again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.