Beware of Uninitialized Primitive boolean Instance Variables

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

Advertisements

About Jeffrey A. Aborot

> Background: BS Computer Science, University of the Philippines Baguio. > Work: Advanced Science and Technology Institute - Department of Science and Technology of the Philippines. > Academics: MS Computer Science (on-going), Algorithms and Complexity Laboratory, Computer Science Department, UP Diliman > Languages: Filipino, Tagalog, Cuyunon, English, Java, Python, C. > Operating Systems: Linux, OSX. > Weird Stuff: Bunch of Pentax film cams.
This entry was posted in Software Development and tagged , , , , , , , . Bookmark the permalink.

6 Responses to Beware of Uninitialized Primitive boolean Instance Variables

  1. luckytoilet says:

    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 😀

  2. JPogi says:

    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.

  3. aboyDaen says:

    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 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s