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.
The second way still retains all the requirements from the first, and is just more concise. (technically speaking it's a form of lazy initialization, since it eagerly initializes on demand).
More reading available here: cs.nyu.edu/courses/fall16/CSCI-UA.0470-001/slides/lecture25.pdf#page=52.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Singleton Pattern
You are viewing a single comment's thread. Return to all comments →
Just saying that from a completeness point of view, something like this would be required:
This utilizes the "double-checked locking" design pattern, and guarentees the following for the singleton:
volatile
keyword there)An even more clever way is this:
The second way still retains all the requirements from the first, and is just more concise. (technically speaking it's a form of lazy initialization, since it eagerly initializes on demand).
More reading available here: cs.nyu.edu/courses/fall16/CSCI-UA.0470-001/slides/lecture25.pdf#page=52.