Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

listener or observer for variable change  RSS feed

 
simone giusti
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am a little bit confused.

This is what I need to do:

I have a variable. I would like to 'do something' when it changes.
I tried several examples with observer and observable but I really did not understand a lot !!

Would something show a really simple example ??

String color = "white!
color = "red"

When color becomes "red" Afunction() has to start automatically ...

that's all !!


Thanks.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simone giusti wrote:
String color = "white!
color = "red"

When color becomes "red" Afunction() has to start automatically ...


I don't think that Observable supports such a capability -- unless something drastically changed since I used it last. To be Observable, changing of state must be done via method code. This means that fields should be private, and setters should be provided. This way, when the setter method are called, your Observable subclass can call notify() to inform all the register Observer of the changes.

Henry
 
Tony lala
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple if else statement ?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has no way to automatically call a method or do something else when the value of a variable changes. What you could do is encapsulate the variable in a class with a getter and setter method. That way, the only way from the outside is to call the setter method - and the setter method can call some other method. For example:

 
simone giusti
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's why I asking in a beginner forum ... since I'm lost between observer ... setter .. getter ... and so on

Would you please provide a simple example ??

thanks ...

Henry Wong wrote:
simone giusti wrote:
String color = "white!
color = "red"

When color becomes "red" Afunction() has to start automatically ...


I don't think that Observable supports such a capability -- unless something drastically changed since I used it last. To be Observable, changing of state must be done via method code. This means that fields should be private, and setters should be provided. This way, when the setter method are called, your Observable subclass can call notify() to inform all the register Observer of the changes.

Henry
 
simone giusti
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, thanks !
so now, how can I change variable ??
do i have to call setColor ?

example.setcolor("blu") ???

... cannot be referenced from a static context


Jesper de Jong wrote:Java has no way to automatically call a method or do something else when the value of a variable changes. What you could do is encapsulate the variable in a class with a getter and setter method. That way, the only way from the outside is to call the setter method - and the setter method can call some other method. For example:

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simone giusti wrote:
example.setcolor("blu") ???

... cannot be referenced from a static context


The example provided by Jesper is an example of an class whose instances has a setter. This means that you actually need to create an instance before you can use it.

Do you know the difference between using an instance and not? Static context and object context? I understand that you are a beginner, so perhaps, you need to understand these concepts first.... as the Observer and Observable classes will require instances to work.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!