File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static object

 
Dev Cunningham
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the best way to create an instance of a Java class that 'globally' available throughout my application? For instance, with C++ I might create an object when my application starts and store a pointer to that object in a global variable.
In this case, I am creating a hash table, which is useful to many parts of my application. Obviously I don't want to create the hash table each time I need to use it.
Thanks!!
 
Greg Brouelette
Ranch Hand
Posts: 144
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do a quick search for "Singleton" on this board.
A "Singleton" object is an object designed in such a way that you can't actually make a new instance of it. You use a static getInstance() method instead. If this is the first time that the getInstance method was called then the object is constructed. On the 2nd and subsequent time that it's called then the method returns the already constructed object.
Here's an example:

[ May 15, 2002: Message edited by: Greg Brouelette ]
 
Dev Cunningham
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Greg!!
That looks perfect.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic