Loading property File using Singleton pattern in Java

Singleton pattern : Singleton pattern insure only a single instance of a class can exist at given time per JVM.

There are different  ways to implement Singleton pattern.

In this example I will provide you to load properties files using Singleton pattern. This implementation is not thread safe. So if you are not dealing with multi-thread you can use this implementation.

public final class PropReader {

 private static PropReader instance = null;
 static Properties prop = null;
 static InputStream in = null;
 public static String tmLocation = null;

 // private constructor so no other class can instanciate this
 private PropReader() {

  try {
   in = this.getClass().getResourceAsStream("/");
   prop = new Properties();
   tmLocation = prop.getProperty("TM_BASE_DIRECTORY");

  catch (Exception e) {

// provide instance if no other instance exist 
 public static PropReader getInstance() throws IOException {

  if (instance == null) {
   instance = new PropReader();

  return instance;


How to write an immutable Class in Java

No comments:

Post a Comment