Skip to content

Instantly share code, notes, and snippets.

@gotoark
Created September 4, 2018 02:02
Show Gist options
  • Save gotoark/0e69b9748a05b1189551438be5673fb6 to your computer and use it in GitHub Desktop.
Save gotoark/0e69b9748a05b1189551438be5673fb6 to your computer and use it in GitHub Desktop.
Singleton Example
package singleton;
public class StaticBlockInitialization {
private StaticBlockInitialization(){}
//1.private Constructor
static{
try{
instance = new StaticBlockInitialization();
}catch(Exception e){
//static block initialization for exception handling
throw new RuntimeException("Exception occured in creating singleton instance");
}
}
//2.Only Instance of the Class
private static StaticBlockInitialization instance;
//3.Public method to enable global access
public static StaticBlockInitialization getInstance(){
return instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment