• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

share an instance but create new one for every new process

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is my problem--

I have 4 classes - Starter , Database and Scheduler and a Test class.

Test class will create a new instance of Starter (which loads and starts the entire process). Starter initializes Scheduler and Database classes.

Test class passes a data to Starter. Starter stores it in a HashMap in Database class. Scheduler reads the same HashMap from Database class.

Now to ensure that the exact same HashMap is access by all classes throughout my java project, I have 2 options-- to make HashMap static or to make Database a singleton class. I have made Database a singleton class for now.

The problem-- if Test class does this

Starter starterInstance1 = new Starter();

Starter starterInstance2 = new Starter();

how do i ensure starterInstance1 and starterInstance2 have their own instance of Database class or the HashMap?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anjan, welcome to the Ranch! We don't have too many rules here, but we do ask that you BeForthrightWhenCrossPostingToOtherSites
http://stackoverflow.com/questions/9139588/share-an-instance-but-create-new-one-for-every-new-process
 
A tiny monkey bit me and I got tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic