I want to observe whether a variable's value has been changed. I was wondering if Java has something that is equivalent to the willSet
and didSet
methods in Swift?
Property observers observe and respond to changes in a property's value. Property observers are called every time a property's value is set, even if the new value is the same as the property's current value. You can add property observers in the following places: Stored properties that you define.
willSet is called before the data is actually changed and it has a default constant newValue which shows the value that is going to be set. didSet is called right after the data is stored and it has a default constant oldValue which shows the previous value that is overwritten.
Not as a language. Java on its own does not really know properties of any kind. What you can do, though:
LongProperty
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With