Singleton Pattern | Learn for Master
  • Five ways to implement Singleton pattern in Java

    In this post, I will describe five ways to implement the Singleton pattern in Java. They are Synchronization the getInstane() method,static final variable, using double checking lock with volatile keyword, using SingletonHolder, and Enum.

    1. Classic Java Singleton pattern

    The following code is the simplest implementation of Singleton Pattern in Java. However it is not multiple thread-safe. 

    The singleton pattern implemented in the above example is easy to understand. We maintain a static reference to the singleton instance and returns that reference from the static getInstance() method. We also make the Constructor private,

    [Read More...]
  • Top java interview questions

    We share some most frequently and important java interview questions.

    Not only are these questions frequently asked by interviewers, but also

    that they are essential for you to master core Java.

    1. How do you implement your own Key Object for HashMap?

      To answer such as question, you probably should also get familiar with the concept of HashTable. Since HashTable use a hash function to map an object to the index of the underlying Array, you need to implement the hashcode method. As there can be conflicts: Two object generate the same hashcode,

    [Read More...]