views:

52

answers:

3

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);
 }
+1  A: 

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).

Jesse
+1 Yup, remove the static modifiers and you should be right to create multiple Matrix objects with different instance variable values. Oh, and also, make sure to change Matrices.xxx = xxx to this.xxx = xxx for all of you set methods
Jacob
Thanks Jacob..I was just adding that to my post. :-)
Jesse
I had it all this.xxx = xxx but eclipse through a fit.. hey if you guys are android, check out Ultimath Beta ;) the matrix multiplication/addition etc will be added on next update i hope :) - in the app market. twitter lifeinjava!
Samuel
Eclipse (and Java) would complain if you still had the static modifier on the class attribute definitions.
Jesse
actually, oddly enough, it was happy when they were static.. idk, thanks for the help anyway
Samuel
"this" cannot be used with a static context.
Jesse
Can you explain to me what a static reference is? I'm getting an error "cannot make a static reference to the non-static method..." Matrices.changeValue(values.get(searching),value)
Samuel
+2  A: 

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

pst
ooh, cool thanks, i figured that was involved because it worked, then i fixed warnings and moved on, then all of a sudden it didnt work.. now, its being a pain in the butt and i cant get it.. thanks!
Samuel
+3  A: 

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.

Cameron Skinner