Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
16 lines (16 loc) · 699 Bytes

File metadata and controls

16 lines (16 loc) · 699 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* Here we described creating singleton using enum but why?
* Let's say in Lazy Init method you access constructor by reflection (Reason being you can access private constructor using reflection!!)
* and create the object which eventually creates the problem by having multiple instances.
*
* ENUM's constructor gets invoked by JVM not by User who is using so it is safe to use.
* Another advantage of using enum is , we don't need to worry about threads as it is thread safe.
* It also solved the problem of Serialization as JVM takes care to return same object.
*
*/
public enum EnumSingleton {
INSTANCE;
public void doSomething() {
System.out.println("Cool");
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.