Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do something when a boolean goes from true to false

 
Jonathan Bjerche
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my "class1" I want to make a method that *listens* to a boolean in another "class2".

Whenever the boolean in "class2" becomes FALSE I want "class 1" to do stuff;


Is it possible to make something like this:



???

How do I make the periodic event?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65113
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not anything near what I'd call a "periodic event", which would be one that repeats at regular intervals.

With regards to the "do something" question you might do some research on the Observer pattern.
 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you make it so class1 can access the boolean variable in class2(by making it public or having them in the same package) you can check quite easily. or you can write a getter. however i think you want class2 to alert class1. you might want to create your own Event but i have never done that so far. i would check out that Observer pattern before i did anything though.
 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YOu could try something like;

 
Campbell Ritchie
Sheriff
Pie
Posts: 49771
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stanley Mungai wrote:YOu could try something like;

I hope that is pseudo-code and not “real” Java™ code, because it will not do what you think it will. You have at least two serious conceptual errors in that code. I shall let you work out what they are for yourself.
 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Ritchie, that was a pseudo-code used it in several occasions.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49771
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using Strings in the first place? Why not simply writeThat will cure both problems which I perceived.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why are you using Strings in the first place? Why not simply writeThat will cure both problems which I perceived.


if (!b) perhaps?
 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you Should try that in a working code and discover first that It doesn't work.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49771
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote: . . . if (!b) perhaps?
Damn! I ought to learn to read!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stanley Mungai wrote:I think you Should try that in a working code and discover first that It doesn't work.


Whom are you addressing and what are you referring to here?
 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both you and Ritchie say for example if we write a code to validate email addresses. If they are not valid we do something, the code would be like:





The above will not work. However, this would work efficiently as I had advised Jonathan Before Ritchie disputed

 
Palak Mathur
Ranch Hand
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan Bjerche wrote:In my "class1" I want to make a method that *listens* to a boolean in another "class2".

Whenever the boolean in "class2" becomes FALSE I want "class 1" to do stuff;


Is it possible to make something like this:



???

How do I make the periodic event?


Try to look into details for java.util.Observer. You will get a simple implementation for Observer pattern.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stanley Mungai wrote:Both you and Ritchie say for example if we write a code to validate email addresses. If they are not valid we do something, the code would be like:




The above will not work.


It works perfectly, although we were talking about using boolean, not Boolean. Either one works just fine though.


However, this would work efficiently as I had advised Jonathan Before Ritchie disputed



No, it doesn't work. And even if it did work, it would be less "efficient" than the boolean approach we are suggesting, but that doesn't matter anyway, because the "efficiency" of the if test is not an issue here. Using String equality to test a boolean or Boolean is bad practice. It's pointless and leads to confusing code.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49771
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stanley Mungai wrote: . . .

The above will not work. However, this would work efficiently as I had advised Jonathan Before Ritchie disputed . . .]
That is different from what you posted earlier, because you are writing Boolean and earlier you wrote boolean. The two are very different.
You are still using the == operator on Strings, which is very dubious practice. I have seen code where == worked nicely the first time it was run and failed miserably after serialisation because the String references had become different.

And you should avoid == false and == true. If you want it to be false, you simply write if (!b) (as I missed earlier!).

The nearest I can think of to a periodic event would be something like thisNote that you would have to alter the value of stillRunning somewhere, otherwise that would be an infinite loop. The two people who suggested you implement an observer were far nearer the mark than that.
 
Wayan Saryada
Ranch Hand
Posts: 105
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about using something like java.beans.PropertyChangeListener? So that a property of a bean changed every object that want to listen to this event will get notified.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or since JavaFX is now bundled with the runtime, if you can rely on the code being run in an up to date JRE (7u3+) you could use a javafx.beans.property.BooleanProperty and add a javafx.beans.value.ChangeListener. Or even use a Binding.
 
Palak Mathur
Ranch Hand
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I Wayan Saryada wrote:What about using something like java.beans.PropertyChangeListener? So that a property of a bean changed every object that want to listen to this event will get notified.


This is also a good option.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic