I need to thank you about this conversation, but I need to update the private constructor code as :
private ApplicationSingleton() {
long currentTime = System.currentTimeMillis();
System.out.println("Singleton instance is created: " + currentTime);
}
And here is the output:
Singleton instance is created: 1347981459285
Singleton instance is created: 1347981459285
Singleton instance is created: 1347981459285
applSingleton1=singlton.enums.ApplicationSingleton@12bc8f01,
applSingleton2=singlton.enums.ApplicationSingleton@3ae34094,
applSingleton3=singlton.enums.ApplicationSingleton@1da4d2c0
What should to mention:
- Singleton instance was created several! times
- All registry objects are different objects with its own data
because we force the private constructor to be public in
c.setAccessible(true);
A value of true indicates that the reflected object should suppress Java language access checking when it is used. A value of false indicates that the reflected object should enforce Java language access checks.
therefore to test the singleton pattern thread-safety you shall use multithreading application