0

Is there any difference in when static class member initialisation is done between the following two situations:

(1)

static ArrayList<String> x = new ArrayList<String>();

(2)

static ArrayList<String> x;
static
{
    x = new ArrayList<String>();
}

As far as I understand it these are effectively equivalent and both guarantee that x is initialised once and once only, and before any class method or constructor can modify it.

1 Answers1

0

First approach is less error prone, for example you can have a static block calling x.get(0); which will produce NullPointerException`

Ori Marko
  • 56,308
  • 23
  • 131
  • 233