PMD report NullAssignment
of the following code, what is the best practice to fix it?
Assigning an Object to null is a code smell. Consider refactoring.
Reached this post while searching for other info, and noticed none of previous answers are right (IMO).
The reason for the null assignment in supplied code is obviously not about garbage collecting, but about being able to stop and recreate new timers, only one timer being able at a time.
I don't see any reason for using the temporary variable on the stopTimer
method, but looks unrelated to original issue.
I would say this code is fine, and a SuppressWarnings("PMD.NullAssignment")
annotation could be added to avoid the violation; PMD is just highlighting null assignments are usually a smell, not a fact of any issue.