tag:blogger.com,1999:blog-8712770457197348465.post1703468176533489070..comments2024-03-19T01:04:48.783-07:00Comments on Javarevisited: 10 Singleton Pattern Interview Questions in Java - Answeredjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger63125tag:blogger.com,1999:blog-8712770457197348465.post-78296677864836595582018-12-16T02:30:37.257-08:002018-12-16T02:30:37.257-08:00Why clone? You cant clone singlton since there is ...Why clone? You cant clone singlton since there is no clone method ( I mean without clonable interface). Why explicitly wrote clonable ?Anonymoushttps://www.blogger.com/profile/11483084921613169646noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-30436395924889979572018-09-01T00:28:02.348-07:002018-09-01T00:28:02.348-07:00Please update the URL for Question "When is S...Please update the URL for Question "When is Singleton not a Singleton in Java". <br /><br />http://www.oracle.com/technetwork/articles/java/singleton-1577166.htmlAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-84415077282447391942017-07-20T02:46:28.906-07:002017-07-20T02:46:28.906-07:00i think,this singleton will work in all situation....i think,this singleton will work in all situation.if any problem with this singleton please suggested to me.<br /><br />public class MySingleton implements Serializable, Cloneable {<br /><br />private static final long serialVersionUID = 5784724829335973182L;<br /><br />private static volatile MySingleton instance;<br /><br />private MySingleton() {<br /><br />if (instance != null) {<br />throw Anonymoushttps://www.blogger.com/profile/17914989972446938977noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-90438761270860642662017-04-12T18:27:09.640-07:002017-04-12T18:27:09.640-07:00Hello @Anonymous, you can throw any runtime except...Hello @Anonymous, you can throw any runtime exception but I suggest to reuse existing one and throw InstantiationException with custom message saying "Cannot create instance of Singleton class".javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-39603269574992413322017-04-12T11:55:57.245-07:002017-04-12T11:55:57.245-07:00You said: to prevent from creating another instanc...You said: to prevent from creating another instance of Singleton using reflection,we can throw exception in constructor. Can you tell me throwing which exception will help here ?<br />By the way, very nice collection of questions.<br /> Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-30702867559964215992016-05-22T09:54:06.946-07:002016-05-22T09:54:06.946-07:00Hi..
In one interview I have asked a question and ...Hi..<br />In one interview I have asked a question and I was not able to answer that. Question was in double check locking we are passing Singleton.class under synchronization. So what is the meaning of Singleton.class? What does this repesent?<br /><br />Could you please answer that.<br /><br />ThanksAnonymoushttps://www.blogger.com/profile/05120449449201373916noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-92141119404216149002015-10-21T19:34:11.583-07:002015-10-21T19:34:11.583-07:00Good questions. If you are looking for some more g...Good questions. If you are looking for some more great Java interview questions from last couple of years, see this list of <a href="http://javarevisited.blogspot.com/2015/10/133-java-interview-questions-answers-from-last-5-years.html" rel="nofollow">13o+ Java interview questions wiht answers</a>Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-67998630327998799062015-07-13T08:54:23.451-07:002015-07-13T08:54:23.451-07:00@Himanshu, you cannot extend a Singleton in Java. ...@Himanshu, you cannot extend a Singleton in Java. They are not designed for Inheritance. Singleton has private constructor which means they cannot be called from any class other than Singleton itself. Since sub class constructor automatically calls super class constructor in Java, its not possible unless the child class is nested inside Singleton itself. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-48337852769422925612015-07-12T11:41:06.547-07:002015-07-12T11:41:06.547-07:00Hi, Can we extend the singleton class in java, if ...Hi, Can we extend the singleton class in java, if yes, then how to achieve to creation a only fixed number of objects of these class. Anonymoushttps://www.blogger.com/profile/13512594495906541395noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-16244237804225009482015-06-22T09:16:05.770-07:002015-06-22T09:16:05.770-07:00Hi,
I have recently asked a question that , we hav...Hi,<br />I have recently asked a question that , we have a parent Class A and a child class B. How can we achieve the singleton in these two class.<br />Please help to find the answer of this question.<br />Thanks in Advance.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-90880434310797201112015-06-04T00:27:50.424-07:002015-06-04T00:27:50.424-07:00Hi All,
Is it possible to write System.println() m...Hi All,<br />Is it possible to write System.println() method instead of System.out.println(). Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-36372022138766918442015-04-15T20:08:28.836-07:002015-04-15T20:08:28.836-07:00Still nobody answered "How to handle singleto...Still nobody answered "How to handle singleton objects in CLUSTERED environment?" I heard that we can use Coherence ( in Memory Caching) and all but no clear idea. Can you please explain ? <br />Dadhihttps://www.blogger.com/profile/07490173058710722606noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-9208917902327091062015-02-14T10:38:08.573-08:002015-02-14T10:38:08.573-08:00How to handle singleton objects in CLUSTERED envir...How to handle singleton objects in CLUSTERED environment?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-43130306200867684572014-09-18T10:30:15.983-07:002014-09-18T10:30:15.983-07:00@Anonymous You can just declare your Singleton in...@Anonymous You can just declare your Singleton instance variable as 'public static' instead of 'private static'.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-1696169037957332652014-09-18T10:25:55.231-07:002014-09-18T10:25:55.231-07:00@Mansi You can see how to use Enum for singletons...@Mansi You can see how to use Enum for singletons in Effective Java: http://www.informit.com/articles/article.aspx?p=1216151&seqNum=3Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-89403173988067432512014-09-18T10:21:56.829-07:002014-09-18T10:21:56.829-07:00@buddy Synchronization of singleton creation is n...@buddy Synchronization of singleton creation is needed in a multi-threaded environment.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-51661144344769912972014-07-29T08:42:03.420-07:002014-07-29T08:42:03.420-07:00The singleton object is the only logic coherence t...The singleton object is the only logic coherence to multiplexing by data!Anonymoushttps://www.blogger.com/profile/05809631362582764177noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-90446236001755658482014-07-29T08:31:58.955-07:002014-07-29T08:31:58.955-07:00@Anonymous should I write you a HOWTO forum?@Anonymous should I write you a HOWTO forum?Anonymoushttps://www.blogger.com/profile/05809631362582764177noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-72492624251895734332014-07-28T08:09:46.420-07:002014-07-28T08:09:46.420-07:00Do we mention that the quoted (non-enum) implement...Do we mention that the quoted (non-enum) implementation is the classic naive-but-actually-broken implementation? http://en.wikipedia.org/wiki/Double-checked_lockingMartin Bartletthttps://www.blogger.com/profile/01709668521765912836noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-25525575645593749402014-07-27T22:56:49.464-07:002014-07-27T22:56:49.464-07:00what is a singleton and when would you use it / no...what is a singleton and when would you use it / not use it? do you think it's a good question to ask an experienced programmer?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-38473621507274852192014-03-06T01:07:34.170-08:002014-03-06T01:07:34.170-08:008. How do you prevent for creating another instanc...8. How do you prevent for creating another instance of Singleton using reflection?<br />In the ans you mentioned : This can be avoided by throwing Exception from constructor as “Singleton already initialized”<br /><br />But this will lead to the exception even for the only object that should have been created of the singleton class via private constructor from the getInstance method itself. The Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-36928293584798215782013-08-09T03:28:50.284-07:002013-08-09T03:28:50.284-07:00Shouldnt we use Enum with a single instance in ord...Shouldnt we use Enum with a single instance in order to prevent it from reflection or the serializable attack.jatinhttps://www.blogger.com/profile/06419923880961837090noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-80450293954340964122013-05-24T23:48:32.644-07:002013-05-24T23:48:32.644-07:00Hi All,
I am not very clear about 8th point, Yes w...Hi All,<br />I am not very clear about 8th point, Yes we can throw exception when we have instance but what if someone trying to create instance 1st time through reflection?<br /><br />Thanks,<br />LakhanAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-6368977198043858682013-04-24T05:03:08.875-07:002013-04-24T05:03:08.875-07:00Is single instance at JVM level or single instance...Is single instance at JVM level or single instance per classloader?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-86348244928680030272013-04-16T19:41:30.776-07:002013-04-16T19:41:30.776-07:00In JP Morgan, I have asked to list down condition ...In JP Morgan, I have asked to list down condition when Singleton fails? I mentioned Serialization, Reflection, multiple ClassLoader, multiple JVM, broken doubled checked locking etc. Does this answer that question?Uttpalnoreply@blogger.com