I'm trying to understand this android implementation, but i just can't figure out why static field lockStatic is volatile, since its use is only through getLock method and it is a synchronized block, so there should be no concurrency or object publishing issues.
abstract public class WakefulIntentService extends IntentService {
abstract protected void doWakefulWork(Intent intent);
static final String NAME=
"com.commonsware.cwac.wakeful.WakefulIntentService";
static final String LAST_ALARM="lastAlarm";
private static volatile PowerManager.WakeLock lockStatic=null;
synchronized private static PowerManager.WakeLock getLock(Context context) {
if (lockStatic == null) {
PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
lockStatic=mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NAME);
lockStatic.setReferenceCounted(true);
}
return(lockStatic);
}
The way i see there would be only the need for volatile if lockStatic was allowed access in any other way, but its private and only method wich deals with it is the getLock method. Is this just an assertion or there is other reasons behind it ?
Thanks in advance for any help resolving this question.