I’m not getting/knowing how to use Setter

Asked

Viewed 60 times

1

I’m trying to do some tests in JAVA here and I’m in trouble:

The product class, with the initialization and the getters/setters

public class Produtos {

    private  String[] nomeItem = {"Item 1","Item 2","Item 3","Item 4","Item 5"};
    private  String[] categoriaItem  = {"Utensílio","Utensílio","Utensílio","Utensílio","Utensílio"};
    private  String[] medidaItem = {"Un","Un","Un","Un","Un"};
    private  String[] dispobinItem = {"5","5","10" ,"10","20"};


    public void iniciaEstoque(String[] nomeItem, String[] categoriaItem, String[] medidaItem, String[] disponibiItem){

        setNomeItem(nomeItem);
        setCategoriaItem(categoriaItem);
        setMedidaItem(medidaItem);
        setDisponibiItem(disponibiItem);

    }


    public String[] getNomeItem() {
        return nomeItem;
    }

    public void setNomeItem(String[] nomeItem) {
        this.nomeItem = nomeItem;
    }

    public String[] getCategoriaItem() {
        return categoriaItem;
    }

    public void setCategoriaItem(String[] categoriaItem) {
        this.categoriaItem = categoriaItem;
    }

    public String[] getMedidaItem() {
        return medidaItem;
    }

    public void setMedidaItem(String[] medidaItem) {
        this.medidaItem = medidaItem;
    }

    public String[] getDisponibiItem() {
        return dispobinItem;
    }

    public void setDisponibiItem(String[] dispobinItem) {
        this.dispobinItem = dispobinItem;
    }

}

The function for inserting a new element:

public class InsereElementos {

    public String[] insereElemento(String[] array, String item){

        int indice;
        int tamanho = array.length;

        String[] arrayDummy = new String[tamanho+1];


            for(indice=0;indice<tamanho;indice++){
                arrayDummy[indice] = array[indice];
            }


            arrayDummy[indice] = item;

            System.out.println(Arrays.toString(arrayDummy));

            return arrayDummy;

        }

}

The main:

public class Principal {

    public static void main(String[] args) {

        String nome = "x";

        String[] dummy = new Produtos().getNomeItem();

        String[] junta = new InsereElementos().insereElemento(dummy, nome);

        new Produtos().setNomeItem(junta);

        System.out.println(Arrays.toString(new Produtos().getNomeItem()));
    }

}

When executing, a syso in the function you insert shows (returns): [Item 1, Item 2, Item 3, Item 4, Item 5, x]

But in the main while trying to give get in the array that should have been modified syso returns:

[Item 1, Item 2, Item 3, Item 4, Item 5]

Where am I going wrong?

Grateful!

1 answer

1

You are always instantiating a new object when you are adding an item and when you are picking up the listing to write. Thus, the item you added in the previous instance is not inserted in the new instance you created to write the list items.

Try something like that:

public class Principal {

public static void main(String[] args) {


        Produtos produtos = new Produtos();

        String nome = "x";

        String[] dummy = produtos.getNomeItem();

        String[] junta = new InsereElementos().insereElemento(dummy, nome);

        produtos.setNomeItem(junta);


        System.out.println(Arrays.toString(produtos.getNomeItem()));
    }
}

With this you keep the same instance of the products object, which you use to insert a new item and then write that new item inserted into your collection

  • Damn man, thank you very much, it’s been difficult to migrate from C to Java, I’ll try to read more not to ask such silly questions! I also appreciate the respect/sympathy, unlike the little fellow there who doesn’t even know what Syso is. Hugs

  • Dude, no problem, never be intimidated to ask anything here, we are here to precisely build knowledge in community. I hope I’ve been able to help you with your question and answer your question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.