How to detect if a variable has changed?
<p>I have found myself wanting to do certain things in my programs only if a variable has changed. I have so far been doing something like this:  </p>

<pre><code>int x = 1;
int initialx = x;

...//code that may or may not change the value of x

if (x!=initialx){
    doOneTimeTaskIfVariableHasChanged();
    initialx = x; //reset initialx for future change tests
}  
</code></pre>

<p>Is there a better/simpler way of doing this?</p>
