I created an Matrix object (like the math Matrix, a 4×4 block of numbers for instance) and it works fine, can set row,col,variable just fine, but, I cant have more than one of the same object, I have it creating an ArrayList of a dozen Matrix objects, each with the three variables, but when I call changeVar(Matrix x,int variable) and refresh the printout of the matrix, it changes all of the numbers to what i changed the variable to. So it looks like its just creating the same instance over and over, and if you change one, it changes them all, am i missing anything obvious?
public class Matrices {
private static int row, col, value, newRow, newCol;
public Matrices(int row, int col, int value) {
this.value = value;
this.row = row;
this.col = col;
}
public static void setRow(int row) {
Matrices.row = row;
}
public static void setValue(int value) {
Matrices.value = value;
}
public static void setCol(int col) {
Matrices.col = col;
}
public static int getCol(Matrices x) {
return col;
}
public static int getRow(Matrices x) {
return row;
}
public static int getValue(Matrices x) {
return value;
}
public static Matrices changeValue(Matrices x, int value) {
newRow = getRow(x);
newCol = getCol(x);
return new Matrices(newRow, newCol, value);
}
}
You are using static members. Static members are shared across all instances.
Don’t do that: remove the ‘static’ keyword.
I am surprised there were no warnings on the
this.x = xlines…Happy coding