2

I can't get from the java documentation if it's possible to listen for a change in object's attribute value. Let's say my object has only one int attribute field. I need to perform an action in the moment, my object changes its state. Is it possible?

Alessandro Sassi
  • 103
  • 1
  • 1
  • 10
  • Possible duplicate of [How to detect if a variable has changed?](http://stackoverflow.com/questions/5401527/how-to-detect-if-a-variable-has-changed) – Ahmad Sanie Mar 25 '17 at 17:37

3 Answers3

6

You want to look into observers/observables in Java. They will allow you to do what you need.

There is a lot online about it.

Here is a little stub: http://docs.oracle.com/javase/7/docs/api/java/util/Observer.html

More here: When should we use Observer and Observable

Community
  • 1
  • 1
cdignam
  • 1,376
  • 1
  • 15
  • 21
  • Your answer seems to be what I need, but after I imported Observable and extend my class with Observable, It still reports me that setChanged() method must be defined. Observable is not the to be extend by my class – Alessandro Sassi Mar 25 '17 at 18:19
  • @AlessandroSassi You need to define setChanged() to be able to notify observers. There is some more info here: http://stackoverflow.com/questions/7271044/observable-in-java and https://docs.oracle.com/javase/8/docs/api/java/util/Observable.html – cdignam Mar 25 '17 at 18:24
  • I did not properly expressed in my premious comment, I yet extend a class in my class, and cannot extend Observable also... – Alessandro Sassi Mar 25 '17 at 18:40
  • @AlessandroSassi Could you give some more detail of your implementation? What are you trying to do? – cdignam Mar 25 '17 at 18:44
  • I have a class which runs on localhost where Objects are constructed, with RMI I access the method of the object on the server class from a client class. I need to print on the console output of the server class a notification string when the client class has accessed the object's methods and modified attributes of the object. – Alessandro Sassi Mar 25 '17 at 18:48
1

If this object isn't yours and you are looking for an event listener of sorts for the property of an object, no, there are no object property listeners.

But, if you can modify the source of the object, I assume you are using getters and setters. So, you'd have something like...

public void setIntegerValue(int num){
    this.num = num;
}

The most efficient thing to do is to add some sort of logic inside the setter to execute your function when the integer num is changed:

public void setIntegerValue(int num){
    this.num = num;
    yourFunction(); // Let your program know that num has been modified
}
AlgoRythm
  • 158
  • 10
  • problem here is you'll have to do it for every setter in the class. If a later programmer adds a new setter and doesn't know about this, things break. There is no way to just detect any changes? – Samuël Visser Dec 03 '19 at 10:39
0

If you use a setter method you can add calls to other methods.

First, make your property private.

private int myProp;

Then, add a setter.

public void setMyProp(int myProp) {
    boolean isVariableChanged = myProp != this.myProp;
    this.myProp = myProp;
    // do other things
}

In this case, if the variable changed, the boolean in the method will be true, which can be used if you want to prevent triggering the method if the variable is changed back to its current value.

You will then also need a getter.

public int getMyProp() {
    return myProp;
}

Generally, getters and setters are recommended to make code more maintainable when the implementation of a class changes.

However, they also give you the ability to use getters and setters as pseudo listeners.

Anish Goyal
  • 2,799
  • 12
  • 17
  • what I'm trying to do is to call a method on an object which is on a server, so with RMI I can see the effect of the mehod invocation on my client console, but I also need to display something on the server console – Alessandro Sassi Mar 25 '17 at 17:40
  • Do you have access to the code on the server? How is that variable set? – Anish Goyal Mar 25 '17 at 17:42
  • Yes I access the code for the server since it's a trivial experiment I'm doing to understand certain things. The variable I'm listening for is set private – Alessandro Sassi Mar 25 '17 at 17:45
  • Ok then at some point you must be calling a setter method to set the value of the variable on the server side. When that method is called, you can call other methods from your setter. – Anish Goyal Mar 25 '17 at 17:49