4. Singleton

Vegeedog
JavaNotes
Published in
Jul 13, 2021

--

Singleton is to ensure there is only a single instance of a class in our app.

To have a singleton of a class, there are few things necessary to do.

  • Create a private & static instance as a field inside the class.
  • Create a method to get the instance (for others to access this instance)

And how to use it?

  • Make sure the constructor is set private, so that others cannot instantiate the class freely.

Whole code:

--

--