Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How can we make a class Singleton
How can we make a class Singleton
✍: Guest
A) If the class is Serializable class Singleton implements Serializable { private static Singleton instance; private Singleton() { } public static synchronized Singleton getInstance() { if (instance == null) instance = new Singleton(); return instance; } /** If the singleton implements Serializable, then this * method must be supplied. */ protected Object readResolve() { return instance; } /** This method avoids the object fro being cloned */ public Object clone() { throws CloneNotSupportedException ; //return instance; } } B) If the class is NOT Serializable class Singleton { private static Singleton instance; private Singleton() { } public static synchronized Singleton getInstance() { if (instance == null) instance = new Singleton(); return instance; } /** This method avoids the object from being cloned **/ public Object clone() { throws CloneNotSupportedException ; //return instance; } }
2013-03-18, 1862👍, 0💬
Popular Posts:
What's difference between HashTable and ArrayList ? You can access array using INDEX value of array,...
How to make elements invisible? Change the "visibility" attribute of the style object associated wit...
What are the five levels in CMMI? There are five levels of the CMM. According to the SEI, Level 1 – ...
What are urlencode() and urldecode() functions in PHP? string urlencode(str) - Returns the URL encod...
How To Enter Numeric Values as HEX Numbers? - MySQL FAQs - Introduction to SQL Basics If you want to...