• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Do something when a boolean goes from true to false

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YOu could try something like;

 
Marshal
Posts: 74342
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Ritchie, that was a pseudo-code used it in several occasions.
 
Campbell Ritchie
Marshal
Posts: 74342
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74342
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74342
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 119
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic