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`

user7294900
  • 47,183
  • 17
  • 74
  • 157