Question: I don't understand why using timer variable like this in the following codes.

Question 1: In both startTimer() and stopTimer(), there is a local variable aTimer to be used before the operation on timer, what is the intention?

Question 2: In stopTimer(), timer will be assigned null, so if timer is not null which means this timer is already created, when startTimer() is called, timer will not be created again. Is this best practice to check whether timer is running or not? By assign null to timer, PMD also reports violation on "NullAssignment"

```
private Timer timer;

private void startTimer() {
  if (timer == null) {
    Timer aTimer = timerFactory.createTimer(100000, 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();
 }
}

```

Answer: 
Your code is equivalent to:

```
private boolean started = false;
private Timer timer;

public synchronized void start() {
    if (!started) {
        timer = timerFactory.createTimer(100000, null);
        timer.setListener(this);
        started = true;
    }
}

public synchronized void stop() {
    if (started) {
        timer.cancel();
        timer.setListener(null);
        started = false;
    }
}

```

which is shorter and easier to understand (IMHO). The local variable is superfluous since your code is synchronized and your `timer` variable can't be accessed by two threads concurrently.

And using the fact that `timer` is `null` as a flag for started/not started is fine with such a short code but as more lines get added, it will become confusing. I prefer a proper `started` flag which is self-explanatory.
The timer is synchronised to prevent thread interference and memory consistency errors. Synchrnizing the timer means:

* No thread interference from other threads
* Ensures Memory consistency
* Ensures Atomic access to the timer, wherein external threads to the timer cannot access the intermediate state of the timer. i.e Either finish the action or have no action at all.