Singleton Pattern

Credits: RefactoringGuru
public class Singleton {//The pattern ensures that there is only one instance of the class and provides global access to it for the outer world.    private static volatile Singleton instance = null; // volatile guarantees the visibility for other threads of writes to that variable    private Singleton(){ }

public static synchronized Singleton getInstance(){
//if you add sync in the method name it will be thread-safe if(instance == null){//lazy instantiating
//the singleton instance is not created until the getInstance method is called for the first time. This technique ensures that singleton instances are created only when needed.
synchronized (Singleton.class){//first instantiation is null and the object will be created once and it will not be kept as null next time and the same object/value will be used in the other calls if(instance == null) {
instance = new Singleton();
}
}
}
return instance;
}

}
Enum Singleton Pattern, Sdets&Updates
File Name in Singleton Pattern, Sdets&Updates

--

--

--

A self-learner in the programming and automation industry. Keep yourself updated in this journey together ;)

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Libraries and Using-for in Solidity

Offshore Software Development Trends 2022

What you (really) can/can’t do with Bubble

BottomSheetDialog Fragment

NgAtlanta is Changing Tech

Qualification Round 2020 — Code Jam 2020

Machine-Learning In Julia is FINALLY Getting Better

To Gantt or not to Gantt?

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
Sdet & Updates

Sdet & Updates

A self-learner in the programming and automation industry. Keep yourself updated in this journey together ;)

More from Medium

Find All Articulation Points in a Graph Using DFS

Useless Java: Day 2

Let’s Learn What is JVM in Java Actually?