java - How to fix pmd violation "NullAssignment"? -
pmd report nullassignment
of following code, best practice fix it?
assigning object null code smell. consider refactoring.
the following code not written me, have question on why creating temporary timer instance, assign instance timer? starttimer
, stoptimer
used in multithread context.
private timer timer; private void starttimer() { if (timer == null) { timer atimer = timerservice.createtimer(default_timer_value, null); atimer.setlistener(this); timer = atimer; } } private void stoptimer() { if (timer != null) { timer atimer = timer; timer = null; atimer.cancel(); atimer.setlistener(null); } } public void start() { synchronized(..) { starttimer(); } } public void stop() { synchronized(..) { stoptimer(); } }
this code written in false believe reference set null
garbage collected faster.
therefore message pmd false believe coded.
this wrong assumption garbage collector runs when memory exhausted , collects objects have no reference left on them.
even calling system.gc()
not cause garbage collector run. call merely hint garbage collector when garbage collector detemines enough free memory avaible not run.
Comments
Post a Comment