Check if a final field is initialized

S

Stefan Z Camilleri

Hi

I am creating a singleton that shall be accessed by multiple threads,
possibly simultaneously... here is my code

public class FooEngine implements FooInterface {

private static final m_barElement;

public static FooInterface getBarInstance() {

synchronized (BarElement) {
if (null == m_barElement) m_barElement = new FooEngine();
}

return m_barElement;
}
}

Somehow I need to check if m_barElement is initialized... in C# I
generally check for null, yet in Java it seems as though I have to
explicitly initialize it to null... but this would then mean that I cannot
assign it to an instance later.

Setting it to an instance of FooEngine at declaration is not an option
since FooEngine requires lots of other preparatory work prior to it's
being instantiated.

So, my question is, how can I check if it has been initialized without
setting it to null?
 
D

Daniel Dyer

Hi

I am creating a singleton that shall be accessed by multiple threads,
possibly simultaneously... here is my code

public class FooEngine implements FooInterface {

private static final m_barElement;

public static FooInterface getBarInstance() {

synchronized (BarElement) {
if (null == m_barElement) m_barElement = new FooEngine();
}

return m_barElement;
}
}

Somehow I need to check if m_barElement is initialized... in C# I
generally check for null, yet in Java it seems as though I have to
explicitly initialize it to null... but this would then mean that I
cannot assign it to an instance later.

Setting it to an instance of FooEngine at declaration is not an option
since FooEngine requires lots of other preparatory work prior to it's
being instantiated.

But it will not be instantiated until the class is loaded. Classes in
Java aren't loaded until they are needed, so you effectively get lazy
initialisation for free. Unless there are other static members of the
class that are accessed before the call to getBarInstance(), the class
won't be loaded until the point at which the getBarInstance() invocation
occurs.

Dan.
 
S

Stefan Z Camilleri

But it will not be instantiated until the class is loaded. Classes in
Java aren't loaded until they are needed, so you effectively get lazy
initialisation for free. Unless there are other static members of the
class that are accessed before the call to getBarInstance(), the class
won't be loaded until the point at which the getBarInstance() invocation
occurs.

Dan.

Thanks Dan... that makes perfect sense!

Thanks again!
 
M

Mustang

This implementation is urgly:
1. Why u declare m_barElement as final?
2. Why u synchronize on the BarElement CLASS? If u synchronize on a
class instead of an object, it leads to that all the access to the
class, including class method invokation, static method invocation,
etc., should wait for the lock before entering the critical area. For
this situation, if another thread invokes a static method of
BarElement, the synchronized block in getBarInstance cannot be entered.
 
S

Stefan Z Camilleri

1- I have to set m_barElement as final since I do not want to risk any
other user of my class to inadvertantly modify the reference. Having it
final I can guarantee access to all threads once I give it to them,
allowing it to be changed is risky.
2- The synchronization was not on the class, that's just the variable name
typed incorrectly, should be m_barElement :) .. yet I've completely
removed the synchroniztion block now and instead am instantiating on
declaration since it was pointed out to me that this is done in a lazy
fashion in java... which serves my purpose perfectly.

Thanks for the input though!
This implementation is urgly:
1. Why u declare m_barElement as final?
2. Why u synchronize on the BarElement CLASS? If u synchronize on a
class instead of an object, it leads to that all the access to the
class, including class method invokation, static method invocation,
etc., should wait for the lock before entering the critical area. For
this situation, if another thread invokes a static method of
BarElement, the synchronized block in getBarInstance cannot be entered..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top