File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Is static initialization of a class varaible guaranteed to occur only once? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is static initialization of a class varaible guaranteed to occur only once?" Watch "Is static initialization of a class varaible guaranteed to occur only once?" New topic
Author

Is static initialization of a class varaible guaranteed to occur only once?

SAFROLE YUTANI
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
In other words, if two threads attempt to create an instance of this class, would the static initialization of ConnectionFactory occur once?
public ConnectionSource {
private static ConnectionFactory factory = new ConnectionFactory();
}
I need exactly one instance of ConnectionFactory.
SAF
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Nope, that will not do what you want.
What you need is a way to check for a second attempt to initialize the reference and prohibit it. This is described by a design pattern known as a Singleton. It works something like this:

This way your object can only be constructed within the class, and the code that executes is subject to a verification.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
To answer the original question - yes, static initialization is guaranteed to occur only once for a given class. However your program does not guarantee that no other code will be able to instantiate a second instance, and that is the problem which Michael's code addresses.


"I'm not back." - Bill Harding, Twister
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
If ConnectionFactory is a class over whose instantiation you have complete control, then using the Singleton pattern may be unnecessary. Appropriate documentation may be sufficient and is cheaper (in terms of byte code and execution time).
This would certainly be the case if ConnectionFactory were a private inner class of ConnectionSource. You might argue that it was still the case if ConnectionFactory were package-private in the same package as ConnectionSource; this depends on how your code is distributed and maintained.
If you want ConnectionFactory to be a public class and to allow code outside its package to instantiate it, but you want to prevent more than one instance being created, then Singleton is definitely the way to go.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is static initialization of a class varaible guaranteed to occur only once?