Add singleton pattern in Java and cpp