tag:blogger.com,1999:blog-8712770457197348465.post5633507514466193552..comments2024-03-17T23:01:26.872-07:00Comments on Javarevisited: Why Enum Singleton are better in Java? Examplesjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger31125tag:blogger.com,1999:blog-8712770457197348465.post-32456373874220774972022-11-25T07:04:40.700-08:002022-11-25T07:04:40.700-08:00Thank you Anonymous, comments like yours motivates...Thank you Anonymous, comments like yours motivates me to keep working :-)javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-61187288517213819122022-10-14T06:36:19.326-07:002022-10-14T06:36:19.326-07:00It is October 2022 and Enum based Singletons are n...It is October 2022 and Enum based Singletons are no longer "new way". This javarevisited blog is priceless. Thank you.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-27717040277526141382020-04-10T01:13:46.041-07:002020-04-10T01:13:46.041-07:00@Aniket Thakur You are right, Why did no one notic...@Aniket Thakur You are right, Why did no one notice that your reply?Anonymoushttps://www.blogger.com/profile/08487360829893004308noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-30238852043284358552018-02-12T08:10:40.805-08:002018-02-12T08:10:40.805-08:00http://www.oracle.com/technetwork/articles/java/si...http://www.oracle.com/technetwork/articles/java/singleton-1577166.htmlAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-31456820215660472812017-09-02T06:01:06.351-07:002017-09-02T06:01:06.351-07:00enum singleton pattern is Lazy Loaded. please corr...enum singleton pattern is Lazy Loaded. please correct the post. see this for proof: https://stackoverflow.com/questions/16771373/singleton-via-enum-way-is-lazy-initializedGouravhttps://www.blogger.com/profile/07650612181243410422noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-24081287118555434362017-04-24T05:26:42.959-07:002017-04-24T05:26:42.959-07:00@Anonymous, Enum Singleton is not lazy loaded. If ...@Anonymous, Enum Singleton is not lazy loaded. If you want a lazy loaded singleton then Singleton holder pattern is best.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-77885902008878214342017-04-24T03:41:32.562-07:002017-04-24T03:41:32.562-07:00Enum singelton is lazy loaded or not?Enum singelton is lazy loaded or not?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-83145444667292169962017-02-18T16:00:54.897-08:002017-02-18T16:00:54.897-08:00We do get different Object of logger in different ...We do get different Object of logger in different class , then how come logger class is considered as singleton.<br />For example , we get logger instance for SomeClass as <br /><br />Logger someClassLogger = Logger.getLogger(SomeClass.class);<br /><br />In AnotherClass logger instance would be:<br /><br />Logger anotherClassLogger = Logger.getLogger(AnotherClass.class);<br /><br />Now, if it Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-22246515477201903762016-10-16T02:39:17.598-07:002016-10-16T02:39:17.598-07:00Looks like , Its taken from Effective Java by Josh...Looks like , Its taken from Effective Java by Joshua Bloch.<br />Due credits should be given :)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-48844829712179012972016-08-29T13:06:02.847-07:002016-08-29T13:06:02.847-07:00A small correction : static is missing from getIns...A small correction : static is missing from getInstance method in double check exampleDouble check lockhttp://singletonjava.blogspot.com/2015/12/java-singleton-design-pattern-double.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-12601130912174026892015-09-09T00:41:31.810-07:002015-09-09T00:41:31.810-07:00"Static" in missing how can you access ..."Static" in missing how can you access the getInstance() method. Double Checking<br /><br />public class DoubleCheckedLockingSingleton{<br /> private static volatile DoubleCheckedLockingSingleton INSTANCE;<br /> <br /> private DoubleCheckedLockingSingleton(){}<br /> <br /> public static DoubleCheckedLockingSingleton getInstance(){<br /> if(INSTANCE == null){<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-50478068680136823542015-07-30T05:31:09.790-07:002015-07-30T05:31:09.790-07:00@Anonymous, INSTANCE object in enum is just one en...@Anonymous, INSTANCE object in enum is just one enum constant of enum type Singleton. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-56262679692697155942015-07-30T05:30:17.557-07:002015-07-30T05:30:17.557-07:00@Anonymous, what is the mistake you see on doubled...@Anonymous, what is the mistake you see on doubled checked locking code? care to explain?javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-31895075557236396722015-07-29T10:17:35.644-07:002015-07-29T10:17:35.644-07:00why the author does not editing double check lock...why the author does not editing double check locking code in the above article, it has some serious mistakes!!!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-7038527558174436112015-04-17T04:09:18.748-07:002015-04-17T04:09:18.748-07:00Please anyone explain INSTANCE object in enum sing...Please anyone explain INSTANCE object in enum singleton exampleAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-77689932208844830882014-06-14T03:30:27.311-07:002014-06-14T03:30:27.311-07:00You cannot declare them locally, within a method.You cannot declare them locally, within a method.valjokhttps://www.blogger.com/profile/05913343468893339183noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-61378237908124532112014-05-09T02:44:00.201-07:002014-05-09T02:44:00.201-07:00@Tschoah: When A steps into the synchronized block...@Tschoah: When A steps into the synchronized block it will check the INSTANCE for null again.<br /><br />Anyway the example is bad, because there is no static modifier on the INSTANCE variable and the getInstance() method.Anonymoushttps://www.blogger.com/profile/07039308173610809785noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-11480794616952125612014-04-07T08:23:29.231-07:002014-04-07T08:23:29.231-07:00Hi there,
This is regarding your example under &q...Hi there,<br /><br />This is regarding your example under "Singleton example with double checked locking". The method getInstance() first checks if INSTANCE hasn't been initialised yet. If not then it steps into the synchronized block and initialises INSTANCE. I see an issue here.<br /><br />If two threads access the method getInstance on the same object and thread A stops right Anonymoushttps://www.blogger.com/profile/05157326564497776544noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-29887522068866216522014-03-09T11:25:57.982-07:002014-03-09T11:25:57.982-07:00In case of Singleton pattern with static factory m...In case of Singleton pattern with static factory method what is the need to declare the Singleton Object as final? If you see java.lang.Runtime class which implements Singleton pattern with early initialization declaration is <br /><br />private static Runtime currentRuntime = new Runtime();<br /><br />Yes as in this case object will be created when class is loaded synchronization is not needed.Aniket Thakurhttps://www.blogger.com/profile/07553743551249736583noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-43333876196554505832013-12-10T19:47:09.958-08:002013-12-10T19:47:09.958-08:00It should be ConnectionManager I guess. Because th...It should be ConnectionManager I guess. Because this method is like constructor, which will be called by Enum while accessed/called.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-59865327373828852832013-12-10T18:02:30.057-08:002013-12-10T18:02:30.057-08:00You have to have "static" modifier in bo...You have to have "static" modifier in both the getInstance() method and volatile variable if you want double checked locking. If method is not static then, you cannot access getInstance by just Singleton.getInstance(), and if the volatile instance variable is not static, then the static getinstance method cannot access the variable.<br /><br />/**<br />* Singleton pattern example with Anonymoushttps://www.blogger.com/profile/11764814797615617980noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-57832963514471030102013-08-21T04:51:40.663-07:002013-08-21T04:51:40.663-07:00we can't have private constructor
private Sing...we can't have private constructor<br />private Singleton(){} Anonymoushttps://www.blogger.com/profile/02094146762167091861noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-2443374298408992272013-08-09T14:59:52.901-07:002013-08-09T14:59:52.901-07:00in last comment, is it ConnectionManagerEnum or Co...in last comment, is it ConnectionManagerEnum or ConnectionManager?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-47722462080757599332013-07-10T02:42:41.624-07:002013-07-10T02:42:41.624-07:00This is the practical way:
public enum Connection...This is the practical way:<br /><br />public enum ConnectionManager {<br /> INSTANCE;<br /><br /> private EntityManagerFactory emf;<br /><br /> private ConnectionManagerEnum() {<br /> emf = Persistence.createEntityManagerFactory("cassandra_pu");<br /> }<br /><br /> public EntityManager getEntityManager() { <br /> return emf.createEntityManager();<br /> }<br />}<br /><br /Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-87092156704452462942013-06-03T23:10:30.086-07:002013-06-03T23:10:30.086-07:00In DoubleCheckedLockingSingleton example, instance...In DoubleCheckedLockingSingleton example, instance variable and getInstance() method must be static. Otherwise, the user has to call object.getInstance() where object is an instantiation of DoubleCheckedLockingSingleton . But the constructor is private; therefore, no one can call getInstance()Anonymousnoreply@blogger.com