Question: I have a Singleton `Logger` class.

```
public class Logger {
  public static Logger INSTANCE = new Logger();

  private Logger() {
    ...
  }
}

```

I want to log in my constructor that a new instance is created. So my code looks like:

```
public class MyClass {
  public MyClass() {
    Logger.INSTANCE.log("MyClass created");
    ...
  }
}

```

I am wondering if this could break static instances of `MyClass`. For instance, if I have:

```
public class MyOtherClass {
  private static MyClass myClass = new MyClass();
  ...
}

```

I fear that this could cause a problem due to undefined order of initialization of static variables. So if `myClass` is initialized before `Logger.INSTANCE` is, then the construction of `myClass` will crash. Is there some mechanism to prevent this from happening or is using static variables in a constructor inherently dangerous? Is there any way to prevent users from creating static instances of `MyClass` in such a case?

Answer: 
```
public class Logger {
  public static Logger INSTANCE = new Logger();

  private Logger() {
    ...
  }
}

```

Is syntactic sugar for

```
public class Logger {
  public static Logger INSTANCE;
  static {
      INSTANCE = new Logger();
  }

  private Logger() {
    ...
  }
}

```

A static block sets up static members of the class, and [is guaranteed to run before your class is used](https://stackoverflow.com/questions/2007666/in-what-order-do-static-initializer-blocks-in-java-run).
No need to worry about breaking anything, JVM will ensure the correct order of construction. However there is a risk multiple instances are created if accessed my many threads. Improve the code my declaring the singleton as final and using an static method to access:

```
public class Logger {
  private static final Logger INSTANCE = new Logger();

  private Logger() {}

  public static Logger getInstance() {
      return INSTANCE;
  }
}

```