I just wanted to implement the enum singleton in java. Here is my implementation where I have tried to create a singleton DataSource instance:
public enum EasySingleton {
INSTANCE;
private DataSource dataSource;
private EasySingleton() {
this.dataSource = MYDB.getDataSource();
}
public DataSource getDataSource(){
return dataSource;
}
}
and get this DataSource instance just like this:
DataSource dataSource = EasySingleton.INSTANCE.getDataSource();
I just wanted to confirm whether my implementation is correct or not. If it is wrong then please tell me how to implement the enum singleton to create a singleton DataSource instance.
MYDBclass as well — at least theMYDB.getDataSource()function. \$\endgroup\$