Uninitialized Objects

I’ve got an object with three arraylists:


public class Foo {

   protected String name;
   protected ArrayList<ObjectA> a;
   protected ArrayList<ObjectB> b;
   protected ArrayList<ObjectC> c;

   public Foo(String _name) {
      name = _name;
   }


Lets say I create a couple instances of this object and for each instance I may initialize and use one or more of the arraylists. Is this inefficient? Will those uninitialized arraylists cause any issues (beyond if I try to use them before initializing them)?

Thanks for any help!
Nathan

Nope, since they are uninitialized they are just pointing to nowhere in memory… there is no hidden memory usage in uninitialized variables (save the space required to store a memory address).

Using uninitialized objects will definitely cause a [icode]NullPointerException[/icode] to be thrown. I’ll usually init them in the constructor or I’ll make an [icode]init()[/icode] method.

I’ll write your example like this.


public class Foo
{

    protected String name;

    protected ArrayList<ObjectA> a = null;
    protected ArrayList<ObjectB> b = null;
    protected ArrayList<ObjectC> c = null;

    public Foo(String _name)
    {
        name = _name;

        // Initialize all vars
        a = new ArrayList<ObjectA>();
        b = new ArrayList<ObjectB>();
        c = new ArrayList<ObjectC>();
    }

}

Thanks for the timely response!

I actually want the arraylists to be uninitialized because many instances of the object won’t be using all of them. So whenever I create a new instance of this object none of the arraylists will be initialized. If an instance will need one or more I will individually initialize those arraylists.

Nathan,

I think you’re trying to optimize memory usage. Right? But keeping them uninitialized and only initializing them on requirement doesn’t provide much difference in execution speed or performance and also increases headache in debugging.

So everytime you want to use them, you’ve to check for null like


if (a == null)
{
    a = new ArrayList<ObjectA>();
}

But writing this at every time you need to access the object ‘a’ will be definitely a headache. Otherwise you get several NPE’s all over your code and will take long time for debugging.

Even if you’ve ensured that you’ve checked all the way in your code, it won’t gain you much performance. If you need to check for this in a loop, then every loop you lose some performance in the ‘if’ clauses while checking for null.

I won’t advise using uninitialized objects. Just initialize them and you’ve found your AC Room.

I would definitely initialize the class members in the constructor for the reasons that a couple of posters have pointed out - if you have to ‘track’ whether they have been initialized and ‘manually’ set them up with some sort of init() method you’re making the code more complex and less maintainable, potentially leading to problems for yourself down the road. It’s almost always much better to worry less about efficiency and more about simplicity and maintainability, until you identify that your code is becoming a bottle-neck.

Obviously from that small code snippet it’s impossible to see exactly how it’s going to be used, but it looks like you’ll probably only have a few of these instances (?) in which case the memory overhead is negligible, and if they’re initialized once the performance impact is also not worth worrying about.

  • stride

First off, thanks for all the good advice.

To go into more detail about the situation. The class in question is the Effect class, which will encompass every imaginable type of effect in my game which could range from various status effects (poison, fire) to enhancements or any sort of magical effect you could think of. Every game object can have one or more Effects on it at any time, though I doubt the number of Effect objects will ever exceed 1000. The three arraylists are potential information that an Effect could need (doubles, GameObjects, and Locations).

If keeping all the arraylists initialized wouldn’t be significant I’ll probably do so.

Even if you will have 10000 entities with a lot of status effects memory shouldn’t be a big problem. :wink:

Premature optimalization is the root of all evil.

By the way, isn’t it better to make class to store status effect data (doubles, GameObjects and Locations) and replace three array lists with single ArrayList? :slight_smile:

Mac70,

Thats what the Effect class is, a class to hold each Effect’s data. Game objects do have an arraylist of Effect objects. If your saying I should have a class that holds a single double, a single game object, and a single location, the issue with that is not all effects have all three of those things and not necessarily in equal amounts. One effect might have 2 doubles, 3 game objects, and a single location.

Nate

Btw

public class Foo {

   protected String name;
   protected ArrayList<ObjectA> a;
   protected ArrayList<ObjectB> b;
   protected ArrayList<ObjectC> c;

   public Foo(String _name) {
      name = _name;
   }

I think it looks nicer:

public class Foo {

   protected String name;
   protected ArrayList<ObjectA> a;
   protected ArrayList<ObjectB> b;
   protected ArrayList<ObjectC> c;

   public Foo(String name) {
      this.name = name;
   }

Nathan,

I bet that there will be no use of that optimizations. The root of evil is the premature optimization. See this page to know when and how to optimize.

Make Java fast: Optimize!

Also, just pointing this out, you three ArrayLists in a Class, not an object. We don’t refer to classes as objects until we actually have an instance of that class in a different class. Are you saying your trying to store all your variables in one class? Don’t do this. Create a couple of classes and add the variables to the class to which they correspond with. Its more OOP.