Dr Heinz M. Kabutz wrote:
> Hi Jeff,
>
> if you throw a RuntimeException, it will automatically be converted to
> an ExceptionInInitializerError and this will prevent your class from
> loading. Subsequent attempts to use the class will produce a
> NoClassDefFoundError, which can be really confusing. You'll be
> searching your class path for missing jar files!
>
> Presumably you want to use this in the context of a Singleton, so I
> would prefer the following code (or move away from statics altogether,
> even better):
>
> public class JeffsCrazyClass {
> private volatile static JeffsCrazyClass instance;
>
> public static JeffsCrazyClass getInstance() {
> if (instance == null) {
> synchronized(JeffsCrazyClass.class) {
> if (instance == null) {
> instance = new JeffsCrazyClass();
> }
> }
> }
> return instance;
> }
>
> private JeffsCrazyClass() {
> try {
> if (ServiceManager.service == null) {
> Context ctx = new InitialContext();
> //perform some lookup from JNDI
> }
> } catch (Exception e) {
> log.log(Priority.ERROR, "Exception"+ e.getMessage());
> throw new IllegalStateException("JeffsCrazyClass could not be
> instantiated", e);
> }
> }
> }
>
> The volatile is important of course, due to the double-checked-locking
> visibility bug.
>
> Regards
>
> Heinz
The first method you're likely to call is "getInstance()", which means putting
the loading in that method wouldn't buy you anything.
Eliminate the overhead of the "if" and "volatile" (which is almost as expensive
as half a synchronized for each read):
public class JeffsCrazyClass2 {
private static JeffsCrazyClass2 instance;
static {
try {
instance = new JeffsCrazyClass2();
} catch(Exception e) {
// instance will be null if an Exception is thrown
Logger.getLogger(JeffsCrazyClass2.class).error("Error in
initializer.", e);
}
}
public static JeffsCrazyClass2 getInstance() {
return instance; // will return null if an error occured
}
private JeffsCrazyClass2() throws Exception {
// do lookup code that could throw an exception
}
}
This also lets the class-loading and initialization system do the locking work
(which it does anyway) for you. You could also write the code using a wrapper:
public class JeffsCrazyClass2 {
private static final Wrapper WRAPPER = createWrapper();
private static abstract class Wrapper {
private abstract JeffsCrazyClass2 get();
}
private static Wrapper createWrapper() {
try {
final JeffsCrazyClass2 c = new JeffsCrazyClass2();
return new Wrapper() {
public JeffsCrazyClass2 get() {
return c;
}
};
} catch(final Exception e) {
return new Wrapper() {
public JeffsCrazyClass2 get() {
throw new RuntimeException(e);
}
});
}
}
public static JeffsCrazyClass2 getInstance() {
return WRAPPER.get();
}
private JeffsCrazyClass2() throws Exception {
// do lookup code that could throw an exception
}
}
This will give you the Exception when you call getInstance(), while the
initialization will always succeed (baring an Error).
Just my 2c worth.
Jason.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CTJUG Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/CTJUG-Forum
For the ctjug home page see http://www.ctjug.org.za
-~----------~----~----~----~------~----~------~--~---