Hi I’ve been looking at any ways if Java can detect if a variable has been modified, is this possible? I want to use a timer so that when variable is modified it will reset back to zero and start up again. If the variable goes unmodified for say 20 seconds (i.e. timer reaches 20 seconds) it will do something like print the variable.
Modified refers to any new value assignments such as array[2] = 3.
Firstly you need to wrap the field in get and set methods, and make it
private. Then you need to make a timestamp that gets updated with eachsetcall:You would then need to monitor
lastModifiedTimewithin a separate thread and trigger an event ifSystem.currentTimeMillis() - lastModifiedTime > 20000.