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.

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

Campbell Ritchie

Marshal

Posts: 56546

172

posted 5 years ago

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 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: 56546

172

posted 5 years ago

1

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.

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.