开发者

Creating an Object [multiple of same instance]

开发者 https://www.devze.com 2023-01-20 17:34 出处:网络
I created an Matrix object (like the math Matrix, a 4x4 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

I created an Matrix object (like the math Matrix, a 4x4 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);
    }
}


The problem is your usage of the "static" keyword.

The short answer is: remove all the static keywords.

The longer answer is that static fields/methods are not associated with any particular instance of the class, so what your code does is set up one field called "row", one called "col" etc and use those for all instances of the class. What you really want is one field per instance: making the fields non-static will achieve this.

In general, avoid using static unless you really, really mean it, i.e. if you want a singleton class, or you have a utility class that shouldn't be instantiated.


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 = x lines...

Happy coding


The "static" modifier on your class fields (i.e.,row,col,value,newRow, and newCol) means that there is one copy of those variables per CLASS rather than per OBJECT.

Further, you are currently modifying those static values in static methods. In addition to removing the static modifier on your class fields, you will need to remove the static modifiers on your methods and then access the object's attributes (fields) using "this" (if your fields have the same names as the method's parameters).

0

精彩评论

暂无评论...
验证码 换一张
取 消