DEV Community

Discussion on: Effective Java Tuesday! Singletons!

Collapse
 
jboschmans profile image
jboschmans

I know I'm late to the party, but I wonder why you would ever use the Singleton pattern instead of just a static class. Doesn't it achieve exactly the same?

Collapse
 
kylec32 profile image
Kyle Carter

I'm not sure I'm following what you mean by "static class." The only static classes come to mind are static inner classes (dev.to/kylec32/effective-java-favo...) which don't solve the singleton problem but instead allows access to a nested class without having a handle on the outer class. Still useful, just not for this problem. Maybe I'm just misunderstanding the question thought.

Collapse
 
jboschmans profile image
jboschmans

Thank you for your response. I guess I didn't explain very well. What I mean is a class where the members and methods are static. This way you can call the static method of the class to do what you want instead of creating an object. eg: ClassName.staticMethod()

Thread Thread
 
kylec32 profile image
Kyle Carter

Thanks for that clarification. Yeah I don't see any problem with that. It seems like just a slight change from option 1. Potentially the downside I see is you don't keep the option of changing how it gets initialized in the future. If that's not needed then you likely are good with this option.

Thread Thread
 
jboschmans profile image
jboschmans

Thank you for your answer!