aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Pre Loaded Class with static values needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Pre Loaded Class with static values needed" Watch "Pre Loaded Class with static values needed" New topic
Author

Pre Loaded Class with static values needed

Vijay Mitra
Greenhorn

Joined: Jul 06, 2001
Posts: 11
Hi,
I'm working on a Swing application , where I would prepare some lookup data (getting data from Data base) and store in some strings , when the application starts for the first time. I would like to access these strings from anywhere in the application i.e should be accessible from multiple classes .
This is required to reduce the overhead time to fetch these static lookup data everytime an user makes a query. Its like caching some useful data during initial load for further use.
Can anyone please guide me as to how I can achieve this. Some samples and examples would be helpful ofcourse.
Thanx.

------------------
SAFROLE YUTANI
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
You need to use a Singleton design pattern. There will be one instance of this class in the JVM, therefore, you can have all methods defined as static and anyone can access the class. You may also need a static initializer that will execute the first time the class is loaded. In this initializer you should populate your class with data from your database that calling classes will ask for. This only occurs once, and therefore subsequent calls to this class will return cached data from static varaibles.
SAF
Vijay Mitra
Greenhorn

Joined: Jul 06, 2001
Posts: 11
Thanx Safrole,
Can you provide me with a rough example code . That would help me a lot . You know this is a new concept to me altogether , an example would be of great help.
I look forward for it.
Thanx again.
SAFROLE YUTANI
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
Just check out "Patterns in Java" by Mark Grand. A singleton design pattern should be in volume 1, maybe Volume 2. I cant give an example because I hate posting code on this board because the indentation always gets screwed up!
Just check out the book and you'll be fine. The Singleton design pattern is very easy to use.
SAF
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pre Loaded Class with static values needed
 
Similar Threads
ejb using datasource best practice
static final variables
XML or Database?
OSCache
Static Strings and the classes that love them...