We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I don't see any problem with synchronization, everything's correct here.
| "The problem with above serialized singleton class is that whenever we deserialize it, it will create a new instance of the class" - this is universal problem, one should not serialize singleton with default serializer :)
EDIT: readResolve() looks like a dirty workaround, but it really should work!
Java Singleton Pattern
You are viewing a single comment's thread. Return to all comments →
Guys, why did my solution with static holder class had some weird assertion? It's definetely the best singleton implementation for Java:
That's the same one I use, and I also get an assertion error :(
I have same error. why?
Not quite 'the best' if you think of synchronization + serialization: http://www.journaldev.com/1377/java-singleton-design-pattern-best-practices-examples . Maybe enum could win some more points.
(but this pattern should be avoided as much as possible)
I don't see any problem with synchronization, everything's correct here.
| "The problem with above serialized singleton class is that whenever we deserialize it, it will create a new instance of the class" - this is universal problem, one should not serialize singleton with default serializer :)
EDIT: readResolve() looks like a dirty workaround, but it really should work!
true... but a sort of || class loaders could harm :)
|| class loaders will handle it, because they synchronize class loading inside of jvm. Nothing to worry about
I think you forgot that str instance string. See the requirements. That's something they use to test this. IDK how.
I avoided str instance for purpose, because I didn't want to spoil whole solution
Aggreed