A
A
Andrey Begin2018-10-28 21:02:08
Java
Andrey Begin, 2018-10-28 21:02:08

Doesn't save data to an object?

In general, there is a BodySky object that stores the type of the body.
However, when I try to change its value through the changeData method , it simply does not change

public static final int MAX_BODYS = 2;

public class BodySky {
    int[] type = new int[MAX_BODYS];
}

public void changeData(int newtype) {
    BodySky bodysky = new BodySky();
    for (int i = 0; i < MAX_BODYS; i++) {
        System.out.println(bodysky.type[i]);
        if(bodysky.type[i] == 0)
        {
            bodysky.type[i] = newtype;
            break;
        }
    }
}

public Main() {
    changeData(2);
    changeData(4);
}

public static void main(String[] args) {
    new Main();
}

That is System.out.println(); always outputs the value 0
I suspect that the error is in the wrong reference to the object.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Romanov, 2018-10-28
@MoVVe

So you always create a new object when you call a method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question