{"id":2139,"date":"2017-04-05T04:38:57","date_gmt":"2017-04-05T04:38:57","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2139"},"modified":"2017-04-05T04:51:20","modified_gmt":"2017-04-05T04:51:20","slug":"how-enum-singleton-works","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/how-enum-singleton-works\/","title":{"rendered":"How ENUM Singleton works"},"content":{"rendered":"<p>Simple Singleton Using ENUM<\/p>\n<p><strong>MySingleton.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic enum MySingleton {\r\n  INSTANCE;   \r\n}\r\n<\/pre>\n<p><em>Enum Classes has Private Constructor by Default<\/em><\/p>\n<p>The Above code can be explicitly written as<\/p>\n<p><strong>MySingleton.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic enum MySingleton {\r\n    public final static MySingleton INSTANCE = new MySingleton();\r\n    private MySingleton() {\r\n    }\r\n}\r\n<\/pre>\n<p>When your code first accesses INSTANCE, the class MySingleton will be loaded and initialized by the JVM. This process initializes the static field above once (lazily).<\/p>\n<p><strong>Why cant enum constructors be protected or public in Java?<\/strong><br \/>\nEnums as a class with a finite number of instances. There can never be any different instances beside the ones you initially declare.Thus, you cannot have a public or protected constructor, because that would allow more instances to be created.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Simple Singleton Using ENUM MySingleton.java public enum MySingleton { INSTANCE; } Enum Classes has Private Constructor by Default The Above code can be explicitly written as MySingleton.java public enum MySingleton { public final static MySingleton INSTANCE = new MySingleton(); private MySingleton() { } } When your code first accesses INSTANCE, the class MySingleton will be&hellip; <a href=\"https:\/\/codethataint.com\/blog\/how-enum-singleton-works\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[227],"tags":[],"class_list":["post-2139","post","type-post","status-publish","format-standard","hentry","category-singleton"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2139"}],"version-history":[{"count":4,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2139\/revisions"}],"predecessor-version":[{"id":2143,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2139\/revisions\/2143"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}