8

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?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
Thor
  • 9,638
  • 15
  • 62
  • 137

1 Answers1

6

Not as a language. Java on its own does not really know properties of any kind. What you can do, though:

  1. If the object is yours, just change your setter. That's why they're in the first place.
  2. If that is not good enough for you and you want something better, at least I hope you have Java 8. In there, you can use JavaFX's properties: e.g. the LongProperty.
  3. If you can't change the code, you'll have to go for AOP.
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145