One place where the Yahoo! guys use this technique is in the YAHOO.util.Anim class. There is a private variable called
isAnimated
. It's a variable that you just don't want users messing with. It only serves to inform the AnimMgr
object. It has an accessor method but that's it.
If the variable were public, and the user decided to set it to false
while an animation was running, the animation would remain in the AnimMgr
queue, forcing the AnimMgr
to loop over it on every frame of the animation, but actually doing nothing. Then, when you wanted to start the animation going again, you would have to set the boolean to true
and then manually restart the AnimMgr
. So it's messy, and it's a two step process later on to restart the thing.
In my opinion, this is the perfect case for a private variable. It's just not a good idea to touch the thing!