*
The moose likes Beginning Java and the fly likes Why is this if-else statement executing twice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why is this if-else statement executing twice?" Watch "Why is this if-else statement executing twice?" New topic
Author

Why is this if-else statement executing twice?

Nikolas Bernadel
Greenhorn

Joined: Aug 09, 2012
Posts: 10

I'm experiencing a rather odd conundrum: a boolean in a class seems to be returning both true and false, which causes an if-else state to execute all contained code.

To begin with, I'm currently holding object TestTasks as a value of a string:

Here's a snippet of the TestTasks class:

Now this is the odd bit. Assume that I've put a new key ("Bob" for example) into the HashMap. Whenever I run this bit of code:

Both lines of code execute, and it seems that the boolean is true after the first execution and false after the second execution.

Is there any possible reason as to why this is happening? I'm using IntelliJ; I thought it was a glitch of some kind.

EDIT: There was an extra setRunning method...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Nikolas Bernadel wrote:
Now this is the odd bit. Assume that I've put a new key ("Bob" for example) into the HashMap. Whenever I run this bit of code:

Both lines of code execute


No, they don't. I guarantee it. You're not observing what you think you're observing.

[EDIT: There was an extra setRunning method...


Are you saying this was the source of the odd behavior, and you've solved your problem? Or is this just an incidental side note?
Nikolas Bernadel
Greenhorn

Joined: Aug 09, 2012
Posts: 10

Jeff Verdegan wrote:
Nikolas Bernadel wrote:
Now this is the odd bit. Assume that I've put a new key ("Bob" for example) into the HashMap. Whenever I run this bit of code:

Both lines of code execute


No, they don't. I guarantee it. You're not observing what you think you're observing.

[EDIT: There was an extra setRunning method...


Are you saying this was the source of the odd behavior, and you've solved your problem? Or is this just an incidental side note?

The if-else statement seemed to execute twice when, in reality, the setter was being called twice. To tell you the truth, I was so frustrated at the time that I thought the issue was the if-else statement itself. I should really have more patience.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Nikolas Bernadel wrote:
The if-else statement seemed to execute twice when, in reality, the setter was being called twice. To tell you the truth, I was so frustrated at the time that I thought the issue was the if-else statement itself. I should really have more patience.


Okay, cool. Glad you got it sorted out.

But yes, when something is making no sense and your faced with something like, "Either (A) X is both true and false, or else (B) I must be making a bogus assumption and something I'm 'sure' about in my code isn't actually what I think it is, even though I know it really really must be," it's always best to pursue option B to the ends of the earth and beyond before seriously considering option A.

It's not that Java is flawless, but it's been looked at for a lot longer by a lot more people than your code or mine has, so the odds are always stacked overwhelmingly in favor of us the programmers making faulty assumptions.
 
 
subject: Why is this if-else statement executing twice?