Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a game that has 3 basic sprites, at the moment I'm loading 8 images into each sprite for animating. Each character class has a sprite object. if I've got 10 characters on screen at once then that's 80 images loaded in to memory. Can I make a central sprite class that only holds 8 images for each of the 3 sprites, then get the character objects to request the relevant images from the central sprite class, thereby massively reducing the memory required for the images?

share|improve this question
Yes, you can, and should. – thedaian Nov 25 '11 at 21:41
Thank you! that's very clear! :-D – bot_bot Nov 26 '11 at 12:47
up vote 1 down vote accepted

You can and you probably should. I believe that in Java (as in C#) objects are going to be passed by reference, so you should get this behavior by default without doing any extra work.

Code like this:

Sprite spr = Sprite.LoadFromFile("c:\sprite.s");

Character c1 = new Character(spr, x1, y1);
Character c2 = new Character(spr, x2, y2);
Character c3 = new Character(spr, x3, y3);

Should give you three Character objects all referencing the same Sprite object.

A quick search shows that I stand corrected. Java is in fact pass-by-value; however, since java has "pointers" the result in the code above should remain the same. Java passes the pointer to the sprite by value; but they will all still "reference" the same sprite.

share|improve this answer
Cool, so I have an image, I pass that image to the character object as a parameter, java passes the value of the pointer (essentially a reference) to the object, which then draws the original image on screen. This means if I have 10 characters all using the same image I simply say "draw this image, ten times in these specific locations" thereby reducing the memory I use significantly. Correct? – bot_bot Nov 26 '11 at 12:44
That is my understanding, yes. – Nate Nov 28 '11 at 7:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.