Java hace una lista de clases con múltiples objetos

I want to create a list of classes with different attributes but i'm facing a problem: when I add a new one, all the others are modify.(the code is really big so i'm posting just a small sample)

public abstract class A{
public static Point Position;
public static int LifePoints;
public static int range;
public static int atackValue;
public static double movementSpeed;

public double getMovementSpeed() {
return movementSpeed;
}
public static void setMovementSpeed(double movement) {//this n funciona
movementSpeed = movement;
}
(...)to make the topic shorter i only show the movement part but class A contains all getters and setters for all movement,position,range,life points and attack value
}

public class B extends A{

public static Point Position;
public static int LifePoints=10000;
public static int range=50;
public static int atackValue=100;
public static double movementSpeed=2;

public A(Point startPoint){
setMovementSpeed(movementSpeed);
setAtackValue(atackValue);
setRange(range);
setLifePoints(LifePoints);
setPosition(startPoint);
}

public class createB(){

(...)

public void create(){

private List<B> list = new ArrayList<B>();

B b = new B(startpoint);

list.add(b);

}

(...)

for(int x=0;x!=list.length();x++){

move(list.get(x));

system.out.println(list.get(x).getMovementSpeed());

}

The println returns the correct value (2.0) for all b's created but everytime I create a new b all the other b's stop moving and the new b moves faster than the previous one. The move function it's made by my teacher and it's confirmed to be working correctly. I suspect the position isn't working correctly and instead of moving the pictures 2 pixels it moves only one X times.

preguntado el 05 de mayo de 13 a las 21:05

Your code has a case of acute "static-itis". Get rid of most all your static modifiers please lest the patient dies. -

remove static from fields -

Also, consider cleaning up your code sample. Most of those fields aren't relevant to your problem, (or rather, are duplicate manifestations of the same basic problem) and it's not indented at all. -

2 Respuestas

That's because you have static fields instead of instance variables.

contestado el 05 de mayo de 13 a las 21:05

El primer punto es que static fields are class-allocated: there is only one field for all instances.

There are other basic mistakes in your sample, though.

  • If you create a subclass, it inherits all fields from its superclass. You do not need to repeat those. That is the whole point of inheritance. Your initial values should be set in the constructor.

  • The constructor of a class should be defined in that class and be named the same as that class. It does not make sense to declare a constructor A dentro de la clase B (I guess that your adjustments for posting this were not quite clean). Your code also seems to have some issues with unbalanced parentheses.

  • You seem to have execution statements outside of a method (or static initialization block, but let's leave that alone), which also does not make sense.

  • As shown, the abstract class A is completely superfluous.

  • Tu método create never seems to return the list it created. How do you want to use it?

  • Use indentation. Standard in Java is 4 spaces of indentation for each nested block.

  • Name your variables properly. A variable should always start with a lowercase letter. Names starting with uppercase letters denote classes.

All in all, I feel a lot of confusion there. You should try to read more code of real projects, to get a feeling for the language(s).

contestado el 05 de mayo de 13 a las 22:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.