Skip to main content
gamedev.net
🔒 Locked

J2ME - member variable initialization?

Started by GekkoCube 2 replies 850+ views
Original Post
GekkoCube
GekkoCube
What is best for a J2ME application: 1) Initializing class member variables at declaration? or 2) Initializing class member variables in the constructor? ---- At first it seems both behaves the same. But after some thought, im not so sure anymore. i am primarily concerned about initalizing memory to null. is it advantageous to set memories to null in the constructor rather than on declaration?? thanks.
markr
markr
If you have multiple constructors, you won't have to initialise those variables more than once if they are initialised at declaration.

This post should be in Java as it has nothing to do with J2ME

Mark
pinacolada
pinacolada
They do behave the same.. I believe all of the initializaion-at-declaration takes place immediately before the code in the constructor (but perhaps *after* super()? not sure on this).

It's a subjective call on which to do. I personally only like to do initialization at declaration for things that have really simple and obvious initial values. This includes setting things to null.

But, if you're going to have some complex formula, or even call a function, then I would put that in the constructor (even though you CAN in most cases put that stuff in the declaration, if you really want to).

The only advantage to picking one over the other is having more organized code.
ibawt
ibawt
There's no point in setting references to null, or 0 as all members are initialized to 0 anyhoo...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.