Results 1 to 7 of 7
Thread: One-time object
- 12-09-2009, 11:20 AM #1
- 12-09-2009, 11:29 AM #2
Read about Singletons.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 12-09-2009, 12:42 PM #3
Mmkay, I've done it and tried to implement some stuff, but I'M getting error msgs and I don't understand them. The Prog:
hxxp://pastebin.ca/1708189
and here are the error msgs:
I've used the official tutorial from sun:Java Code:javac "MySingletonTest.java" (in directory: /media/DATA AND SH/uni/semester_3/inf_iii/uebeungsblaetter/a_7) MySingletonTest.java:3: cannot find symbol symbol : constructor MySingleton() location: class MySingleton private static MySingleton _instance = new MySingleton(); ^ MySingletonTest.java:34: MySingleton(java.lang.String) has private access in MySingleton MySingleton IAmSingleWhatsWithYou = new MySingleton ("uk"); ^ 2 errors Compilation failed.
hxxp://java.sun.com/developer/technicalArticles/Programming/singletons
what to do
- 12-09-2009, 09:25 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 24
- Rep Power
- 0
this is one way of implementing the Singleton pattern...Java Code:public class Elvis { private static Elvis instance; private Elvis() { } public static Elvis getInstance() { if(instance==null) instance = new Elvis(); return instance; } }Last edited by wtd_nielsen; 12-09-2009 at 09:28 PM.
- 12-10-2009, 09:05 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
That way is not threadsafe.
The more common technique is:
This ensures no threading issues.Java Code:public class Elvis { private static Elvis instance = new Elvis(); private Elvis() { } public static Elvis getInstance() { return instance; } }
Obviously in a stand alone, single thread, application it doesn't matter, but it's good to get used to using the above anyway. Makes sure you don't accidentally do the non-threadsafe one in a webapp, for example.
- 12-10-2009, 10:40 AM #6
Member
- Join Date
- Dec 2009
- Posts
- 24
- Rep Power
- 0
thats true, it depends on what you want. there is also a solution that combinds lazy initialization and thread safety..
I forgot that this was in the Threads and Synchronization forum.. :)Last edited by wtd_nielsen; 12-10-2009 at 10:43 AM.
- 12-10-2009, 02:04 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Operator < cannot be applied to java.lang.Object, Object
By Albert in forum Advanced JavaReplies: 2Last Post: 11-26-2010, 02:12 AM -
Class Time - represents time of day
By verbazon in forum New To JavaReplies: 1Last Post: 04-13-2009, 01:06 AM -
Time constraints on object state
By Flipke in forum New To JavaReplies: 4Last Post: 04-11-2009, 06:12 PM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM -
Parsing a superclass object to subclass object dynamicly
By Andrefs in forum Advanced JavaReplies: 1Last Post: 07-22-2008, 04:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks