In Java, uninitialized primitive boolean instance variables, or member variables, have the value of false by default. This would seem harmless until we fall into the trap of always-resulting-to-false-condition-and-i-dont-know-goddam-why bug..whew! that was long T_T
Imagine you have this quite big source code with, let’s say, 2000 lines of code and in the top part of your code you have this uninitialized primitive boolean instance variable. Now somewhere along line 1,700ish of your code you have a logic which tries to do something based from the value of your boolean instance variable (which you forgot to update or explicitly assign a value to). It could be something like this:
1. public class QuiteBigClass {
2. private boolean bool; //your uninitialized primitive boolean instance variable with the default value of false
… //some reeeeally long code goes here
1700 if ( bool ) {
1701 //some important code goes here..like committing to the database
1702 } else {
1703 //some other code goes here…
1704 }
… //some reeeeally long code goes here..again
2000 }
Now, given that we forgot to explicitly assign a value to bool, the if-else statement at line 1700-1704 will always result to executing the else part of the source code because the value of bool is false. It will be easy for us to trace the source of the bug if we only have a couple of few lines but given that we have quite a big source code, it will somehow be difficult for us to trace back the origin of the problem, which is an uninitialized primitive instance variable.
One solution to this problem is to always always and always initialize your instance variables to something, in this case, to either true of false. Also, never never and never forget updating the value of your instance variables somewhere in your source code, that way we know that we are not trying to evaluate something based from a variables default value, but from previous logical conditions.
That’s all for now. Thank you.
Continually learning,
Jep
But isn’t it exactly the same as initializing it to false? If you explicitly initialize it, I think you would have the exact same problems.
hehe..yes i will agree with that..still, i believe that explicitly assigning an initial value to it will leave a mark on your mind that it has an actual value..atleast that is how it works for me 😀
it is advisable to initialize an object only when you’re goin’ to use it or it’s required in the code. this way, you could somehow lessen operations in terms of memory.
well then i guess I’ll just cross my fingers and pray that that bug won’t come out again..at least to me..hehehe 😀
Ano raw? hehe. just discovered your blog, jep. hindi ko man maintindihan ang iba, iintindihin ko ang matitira. 🙂
kuya aboy!! musta musta? hehe 😀 napadpad ka sa sulok ko.. 🙂 wag kang mag-alala kuya di lang ikaw ang nagsabi sa akin nyan..hahaha! 😀 kaya I’m starting to convert it to a photoblog para mas maraming makarelate sa mga iniisip ko..hehe 😀 oh kelan na ang photowalk natin? sama mo si kuya mark 😉