wood burning stoves*
The moose likes Beginning Java and the fly likes Do something when a boolean goes from true to false Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Do something when a boolean goes from true to false" Watch "Do something when a boolean goes from true to false" New topic
Author

Do something when a boolean goes from true to false

Jonathan Bjerche
Greenhorn

Joined: Nov 14, 2011
Posts: 9
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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.


SCJP
Visit my download page
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

YOu could try something like;



Give a beggar a fish; feed him for a day. Teach him how to fish; Feed him for a lifetime.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Dec 09, 2011
Posts: 155

Yes Ritchie, that was a pseudo-code used it in several occasions.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Why are you using Strings in the first place? Why not simply writeThat will cure both problems which I perceived.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Dec 09, 2011
Posts: 155

I think you Should try that in a working code and discover first that It doesn't work.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Jeff Verdegan wrote: . . . if (!b) perhaps?
Damn! I ought to learn to read!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Dec 09, 2011
Posts: 155

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

Joined: Jan 29, 2007
Posts: 313

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.


Palak Mathur | My Blog | TechJaunt | What is JavaRanch? | List of All FAQs
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Feb 05, 2004
Posts: 105

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.


Website: Learn Java by Examples
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Do something when a boolean goes from true to false