java - Array overwriting all other values when a new one is added -
i have been having issue when adding objects array. seems every single time add new woodfloor object array, overwrites of other values of array. here's code:
package code; public class main { private static block[] blocks = new block[12]; public static void main(string[] args) { for(int = 0; < 12; i++) { blocks[i] = new woodfloor(i * 10, * 20); } } } package code; public class block { protected static int x, y; public block(int x, int y) { this.x = x; this.y = y; } public int getx() { return x; } public int gety() { return y; } } package code; public final class woodfloor extends block { public woodfloor(int x, int y) { super(x, y); } }
don't use static modifier class fields need different each instance. static modifier makes field class field, 1 shared instances, , not want.
so change this:
protected static int x, y;
to this:
protected int x, y;
Comments
Post a Comment