-10

In this method:

 private void implementVisibility(EditText A, EditText B, ImageButton C, boolean visible) {
   if (visible) {
     A.setVisibility(View.VISIBLE);
     B.setVisibility(View.VISIBLE);
     C.setVisibility(View.VISIBLE);
   }
   else if (!visible) {    // warning here
   }
 }

At the else if (!visible) line I get the warnings:

Condition !visible is always true
Value visible is always false

But looking at the calling method:

if (count2.getVisibility() == View.INVISIBLE) {
  implementVisibility(count2, action2, remove2,true);
}

visible is true, therefore it isn't always false. Why doesn't the compiler notice this?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117
  • 3
    Just use **else** instead – B001ᛦ Jul 08 '16 at 09:25
  • 1
    Just read your code. First you are asking "is this true"; and if that is not the case you are asking "is this not true". Hint: you don't need that second question; that is the idea of boolean - it is either true or false. And hint: A, B, C, are really poor names for variables. The law prohibiting to use more than one character per variable name was abolished late 1979 I think ;-) – GhostCat Jul 08 '16 at 09:26
  • there is just two possibilities in boolean, So if and else statement will be enough – Arun Shankar Jul 08 '16 at 09:31
  • Do you mean, at the line:implementVisibility(count2, action2, remove2,true); the compiler must notice that the last param is always true? – berserk Jul 26 '16 at 07:41
  • @berserk it says that the Boolean is always false, yet in one method I pass that boolean as true – Ali Bdeir Jul 26 '16 at 07:43
  • @AbAppletic It doesn't say that the boolean is always false. I says !visible will always be false in else statement, because the control will go to else statement only when if statement fails. Now if statement will fail only when visible is false. So, whenever else statement is executed, visible will be always false, hence, !visible will be always true. – berserk Jul 26 '16 at 08:09

2 Answers2

4

By the time program control gets to else if(!visible){, visible can only be false.

Your compiler is hinting that you should clarify your code to else { instead.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
-1

Boolean variable only have 2 values true or false, so that you should edit your code as belows:

private void implementVisibility(EditText A, EditText B, ImageButton C, boolean visible) {
        if (visible) {
            A.setVisibility(View.VISIBLE);
            B.setVisibility(View.VISIBLE);
            C.setVisibility(View.VISIBLE);
        }
        else {
            // do something
        }

    }
Dang Nguyen
  • 354
  • 2
  • 9