File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Singleton Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Singleton Class" Watch "Singleton Class" New topic

Singleton Class

Chris Ramsey
Ranch Hand

Joined: Sep 15, 2003
Posts: 34
Hi, I have a dataclass that I want to be a singleton class. This is how it looks...

This part works okay. I get one instance and use it to access the methods I need. The problem is I want to get a snapshot of the data each time the load method is called and dump it into an array list. I want each item in the list to store the different member values that were loaded. I am reading the values from a file. This is the way I am attempting to store a snapshot of the SingletonClass each time its members are updated with the load() method.

My first load may have loaded (100) and my second load(200) but if I call "ArrayList.get( 0 )" the value for load 2(200) is returned.
The reason I went this way is because it processed 100000 lines of pipe-delimited values in about 2234 ms. and I don't have to create a new dataclass instance for each line. Is there something I am leaving out? Is this a possibilty using the Singleton approach? Thanks for you help and patience.
[ January 13, 2004: Message edited by: Chris Ramsey ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You're seeing the last value from every entry in the arraylist because every time you add the very same object (the single instance) to the arraylist. You might wind up with a hundred references to the same object and they'll all give the same result. So, you don't want to store the SingletonObject itself in the arraylist, but the current value that's in it. You want something more like:

Does that make sense? Bad news is it doesn't work yet. You can't add an int to an ArrayList because it's not an object. So maybe add new Integer(gettheint...).
Something to ask yourself about any singleton, does it really have state that we need to keep? The only value being kept for the long term are in the array list so the answer seems to be no. In that case, you can use all static methods and variables and never create an instance at all. Of course you may have other things going on that make the singleton more interesting.
Either approach - singleton or static - can run into trouble if you have multiple threads. Probably not a problem today, but something to keep in mind when you get to threads. Then you'll have to synchronize methods or give up and make mulitple instances.
Last note: Your getInstance method tests for null, but it will never be null because you created an instance along with the declaration. That's a good way to do it! You can just remove that test.
Hope all that helps!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Chris Ramsey
Ranch Hand

Joined: Sep 15, 2003
Posts: 34
Thank you Stan. Yes that makes sense. I should have caught that. So maybe I should do something like...

This will work. The rest of the program will parse the line to some textfields. I guess I would have to create the string at some point, but atleast it will be ready to present. I just didnt want to create a whole bunch of strings. Thank you very much for responding.
[ January 13, 2004: Message edited by: Chris Ramsey ]
I agree. Here's the link:
subject: Singleton Class
It's not a secret anymore!