Memory question

If I have an object that has a variable containing an image, the image will be saved for each object I create even if it’s always the same image. Right? (unless I make it static)

What I meant is that for each instance of the object, the entire image data will have to be save in memory and not just an address referencing to that image?

Yes. :smiley:

EDIT: I wanted to expand on this: when you read an image in Java, it decodes it and stores all the raw data in Raster (actually in DataBuffer, which Raster has an instance to) as integer values for each pixel. So each time you read any image (even if it’s the same one), it uses a new memory address to store those values in the objects.

You can read the image once, and remember the Image or BufferedImage created for it. Then every time you have an object that needs that image content, use the already created Image object. The actual image data is in memory once and you have multiple references to the image.

Create a class that hold all resources. When an instance want to get an image, ask that class if it’s available. If yes, return it.

That’s what I do until now :smiley: