java - Setting counter value back to initial in class -
java - Setting counter value back to initial in class -
public class decreasingcounter { private int value; // instance variable remembers value of counter int valueinitial; public decreasingcounter(int valueatstart) { this.value = valueatstart; } public void reset(){ this.value = 0; } public void setinitial(){ this.valueinitial = 0; } }
i need alter value in decreasingcounter(x);
setinitial();
using object variable, don't know how.
it seems valueinitial
:
public decreasingcounter(int valueatstart) { this.valueinitial = valueatstart; this.value = valueatstart; } public void setinitial(){ this.value = this.valueinitial; }
you store initial value in constructor (assigning this.valueinitial
) , restore in setinitial()
.
you don't want set this.valueinitial
0
, since cause lose initial value set in constructor.
java class
Comments
Post a Comment