How to retrieve information from an object I put into an arraylist

I created a class:

public Inventario (int x, int y, int width, int height, BufferedImage sprite, String frutas) {}

This classe is to show an image (with width and height) at the x, y point on the screen. The sprite is the image itself and the String is my attempt of getting information by the name of the image saved.

So I add some objects to this class and put each object into an array list called “itensInventario”:

 Inventario ban = new Inventario(63, 27, 6, 6, sheet.getSprite(121, 171, 6, 6), "banana"); 

Inventario cen = new Inventario(10, 96, 6, 6, sheet.getSprite(121, 178, 6, 6), "cenoura");  

The question is: how do I retrieve the String name of each object I put into the arraylist?

I tried to do something like this:

        for(int i=0; i< Game.itensInventario.size(); i++) {
        System.out.println("Show: " + Game.itensInventario.get(i));}

But I don’t know how exactly get the String for each index “i”.


Inventário class (maybe it doesn’t help. I tried to make a method GetFruta but in the end all objects would be with the same name in String so I gave up it but it still there):

public class Inventario


public int dx;
public int dy;
public BufferedImage [] itens;  

public static String fruta;

public Inventario (int x, int y, int width, int height, BufferedImage sprite, String frutas) {

    itens = new BufferedImage [2];  
    itens[0] = Game.sheet.getSprite(121, 171, 6, 6); //Banana
    itens[1] = Game.sheet.getSprite(121, 178, 6, 6); //Cenoura
    fruta = frutas;


public void setFruta(String novaFruta) {this.fruta = novaFruta;}
public String getFruta() {return fruta;}

public void tick() {

    for(int i=0; i< Game.itensInventario.size(); i++) {
        System.out.println("esse: " + Game.itensInventario.get(i)+ " index: " +Game.itensInventario.get(i)+ "nn");}

public void render(Graphics g)
        if(Game.itensInventario.size() >0) {
  if( this==Game.itensInventario.get(0) ){         
       if(Game.itensInventario.get(0).getFruta() == "banana") { g.drawImage(itens[0], 63, Game.HEIGHT-27, null); }
       else if(Game.itensInventario.get(0).getFruta() == "cenoura")  { g.drawImage(itens[1], 63, Game.HEIGHT-27, null); }        
   if(Game.itensInventario.size() >1) {
  if(this==Game.itensInventario.get(1) ){          
       if(Game.itensInventario.get(1).getFruta() == "banana") {  g.drawImage(itens[0], 70, Game.HEIGHT-27, null);}
       else if(Game.itensInventario.get(1).getFruta() == "cenoura")  { g.drawImage(itens[1], 70, Game.HEIGHT-27, null); }        




I created this way the arraylist (List of objects from the class Inventario, ecah object has a String, right? Once I put this String in a new object how do I retrieve it?):

 public static List<Inventario>itensInventario;
 itensInventario = new ArrayList<Inventario>();


If you’re trying to get your frutas string, then it is as simple as


However at the moment your fruta field in the Inventario class is static, which means it doesn’t belong to that specific instance and is instead a shared variable amongst all instances. So removing the static modifier should solve your problem.

I would recommend taking a look at a Java tutorial to get a good grasp of some of the basics on static variables, like here: Understanding Class Members

Leave a Reply

Your email address will not be published. Required fields are marked *