Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Uninitialized Objects  (Read 572 times)
0 Members and 1 Guest are viewing this topic.
Offline penguin_buddha

Senior Newbie





« Posted 2013-07-29 16:53:18 »

I've got an object with three arraylists:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #1 - Posted 2013-07-29 16:56:46 »

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).

Online SHC
« Reply #2 - Posted 2013-07-29 16:57:27 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online SHC
« Reply #3 - Posted 2013-07-29 17:02:56 »

I'll write your example like this.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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>();
    }

}

Offline penguin_buddha

Senior Newbie





« Reply #4 - Posted 2013-07-29 17:17:46 »

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.
Online SHC
« Reply #5 - Posted 2013-07-29 17:29:54 »

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

1  
2  
3  
4  
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.

Offline StrideColossus
« Reply #6 - Posted 2013-07-29 17:38:05 »

I've got an object with three arraylists:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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

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
Offline penguin_buddha

Senior Newbie





« Reply #7 - Posted 2013-07-29 21:33:23 »

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.
Offline Mac70
« Reply #8 - Posted 2013-07-29 22:43:22 »

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<StatusEffect>? Smiley

Check out my Devblog! Smiley
Offline penguin_buddha

Senior Newbie





« Reply #9 - Posted 2013-07-29 23:21:14 »

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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Zhon

Junior Member


Medals: 4



« Reply #10 - Posted 2013-07-29 23:34:26 »

Btw
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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;
   }
Online SHC
« Reply #11 - Posted 2013-07-30 07:20:32 »

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!

Offline opiop65

JGO Kernel


Medals: 123
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #12 - Posted 2013-07-30 12:17:28 »

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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (24 views)
2014-04-15 18:08:23

BurntPizza (19 views)
2014-04-15 03:46:01

UprightPath (33 views)
2014-04-14 17:39:50

UprightPath (17 views)
2014-04-14 17:35:47

Porlus (33 views)
2014-04-14 15:48:38

tom_mai78101 (59 views)
2014-04-10 04:04:31

BurntPizza (117 views)
2014-04-08 23:06:04

tom_mai78101 (217 views)
2014-04-05 13:34:39

trollwarrior1 (184 views)
2014-04-04 12:06:45

CJLetsGame (191 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!