Follow these steps to implement the example:
- Create a class named DBConnectionOK:
public class DBConnectionOK {
- Declare a private constructor. Write the name of the thread that executes it:
private DBConnectionOK() { System.out.printf("%s: Connection created.\n", Thread.currentThread().getName()); }
- Declare a private static class named LazyDBConnectionOK. It has a private static final DBConnectionOK instance named INSTANCE:
private static class LazyDBConnection { private static final DBConnectionOK INSTANCE = new DBConnectionOK(); }
- Implement the getConnection() method. It doesn't receive any parameter and returns a DBConnectionOK object. It returns the INSTANCE object:
public static DBConnectionOK getConnection() ...