Double Brace initialization

It creates an anonymous class derived from the specified class (the outer braces), and provide an initializer block within that class (the inner braces). e.g.

new ArrayList() { { 
   add("you"); 
   add("me"); 
} }

Bill Pugh Singleton Implementation

We all know different flavors of singleton design pattern and we are using one of them in our daily life.

Eager Initialization
Static Block initialization (type of eager initialization but we initialize the object in static block with exception handling)
Lazy Initialization
Thread Safe Singleton
Double checked locking (to overcome the race condition)
Enum Singleton (Enums are lazily initialized by JVM and enums are thread safe too)

Here I will discuss one approach which I didn't Include in above approaches

Bill Pugh Singleton Implementation

Prior to Java, 1.5  Java Memory Model had a lot of Issues and the above Approaches used to fail in certain scenarios where too many threads try to get the instance of singleton class simultaneously.

So this Implementation comes with a different approach where it creates singleton class using the inner static helper class.

Implementation will be like : -
public class BillPughSingleton {
    private BillPughSingleton(){ }
    private static class SingletonBuilder {
           private static final BillPughSingleton INSTANCE = new BillPughSingleton();
    }
    public static BillPughSingleton getInstance(){
           return SingletonBuilder.INSTANCE;
    }
}


The best thing about this approach is when the Singleton class is loaded, SingletonBuilder will not be loaded and only loaded when someone will call getInstance() method, and it doesn't require synchronization too.

Functional programming with Java - Part 1

 Recently I was reviewing one PR raised by team memeber and going through one utitlity method and found out there are too many muatable vari...