Singleton Pattern in Java

This has the same purpose as static variable in java. Can be initialized only once!

Problem solved :

When to use?

  • The class, for which we want to create only one object or instance. (Logger, Database etc)
  • In Firebase, database instance is created only once:
Android :*
Firebase getInstance() is singleton implementation.

Code Findings:

Redux-Store is an singleton state tree.
  • It is used in react-redux (I believe). If you see, the redux store is initialized only once.



redux store is apparently the single point of state data.

Example again:

Suppose your back-end is live. During some point in time, there where some Exceptions thrown. In the end of the day, these Exceptions are to be pushed into the DB and analysed.

For this scenario, we need to keep a single object(Exception Queue) to hold Exceptions thrown from various Classes, rather than, instantiating a new Exception Queue every time.

Project Structure:

Implementation in Java:

  • private constructor
  • public Class getInstance() calls the private constructor

2. Driver to test the program

Found it Interesting?
Please show your support by 👏. To read the complete series, click below.


I myself, has just started learning, design patterns. If you find any issues please feel free to post them in the comments section below. Thank you for reading so far 😄

Writes on Big Data, AWS & Backend technologies.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store