2

I need to assign multiple boolean variables to false in java (As a part of my Android Application)

What is the best way to do that?

Is the following method correct? Does it have any drawbacks/pitfalls?

boolean showDownloadButton, showOpenFileButton, showProgressBar, showErrorMessage, showDownloadWhenReady;

showDownloadButton = showProgressBar = showOpenFileButton =  showErrorMessage = showDownloadWhenReady = false;
2
  • 2
    Have you tried it? What was the result? Commented Jul 11, 2015 at 17:16
  • 2
    As another comment, if these booleans are class members, they are automatically initialized to false. If they are not class members, then your design is probably wrong. Commented Jul 11, 2015 at 17:16

1 Answer 1

1

There is no drawback or advantage to this, because each individual variable is set separately anyway. Setting it to boolean literal false or to a result of another assignment returning false makes no difference, apart from the readability.

Note that you could potentially simplify your code by making an array of booleans instead, and using constant indexes to access individual values:

static final int DOWNLOAD_BTN = 0;
static final int OPEN_FILE_BTN = 1;
static final int PROGRESS = 2;
static final int ERROR_MSG = 3;
static final int DOWNLOAD_WHEN_RDY = 4;
static final int ELEMENT_COUNT = 5;
...
boolean[] visibility = new boolean[ELEMENT_COUNT];
...
if (visibility[ERROR_MSG]) {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.