boolean listIsEmpty = myList.size()==0;
Will listIsEmpty
change to false as soon as the size of myList
changes of is this a one-time assignment?
If the latter, can I have a 'dynamic' boolean that 'watches' myList?
boolean listIsEmpty = myList.size()==0;
Will listIsEmpty
change to false as soon as the size of myList
changes of is this a one-time assignment?
If the latter, can I have a 'dynamic' boolean that 'watches' myList?
Simple answers: no and no.
The expression is evaluated once and then the result in your boolean
will not change.
To have such a 'dynamic' boolean
you have to create your own list with a listener that will achieve his.
No, because this is not defining a mathematical relationship, it is defining a set of instructions. After the instructions are executed, one cannot expect listIsEmpty
to automaically update.
To do automatic updates, look into the "Listener Pattern".
To write this condition in a more sensible manner, it is the List's responsibility to know if it is empty, which is why it contains an isEmpty()
method. Instead of caching this information and acting on the cache, delegate the request.
if (myList.isEmpty()) {
... do stuff ...
}
Will prevent the cache from returning a stale view of the list's emptiness.
boolean listIsEmpty() {
return myList.isEmpty();
}
A variable assignment is evaluated at that spot.
Using a function like above, if (listIsEmpty()) { ... }
evaluates the function (method) body everytime.
Your ListisEmpty
varible is not gonna change untill you change it yourself. you have to update your ListIsempty
variable each time you update your Mylist
,