Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here is some of my code:

           PlayerInfo P1 = new PlayerInfo();
            P1.setInfo(1);
            System.out.println("" + P1.X + "," + P1.Y);
            PlayerInfo P2 =  new PlayerInfo();
            P2.setInfo(2);
            System.out.println("" + P1.X + "," + P1.Y);
            PlayerInfo P3 = new PlayerInfo();
            P3.setInfo(3);
            System.out.println("" + P1.X + "," + P1.Y);
            PlayerInfo P4 = new PlayerInfo();
            P4.setInfo(4);
            System.out.println("" + P1.X + "," + P1.Y);

Player Info is defined as:

public class PlayerInfo{
public static int Range;
public static int X;
public static int Y;
public static int Score;
public static int Lives;
private static ImageIcon image;
public PlayerInfo(int Num){
    Range = 1;
        if(Num == 1){
            this.X = 0;
            this.Y = 0;
            //System.out.println("" + X + "," + Y);
            image = new ImageIcon("H:\\My Pictures\\BomberMan\\BMBlack.png");
        }
        else if(Num == 2){
            this.X = 16;
            this.Y = 0;
            //System.out.println("" + X + "," + Y);
            image = new ImageIcon("H:\\My Pictures\\BomberMan\\BMWhite.png");
        }
        else if(Num == 3){
            this.X = 0;
            this.Y = 16;
            //System.out.println("" + X + "," + Y);
            image = new ImageIcon("H:\\My Pictures\\BomberMan\\BMRed.png");
        }
        else if(Num == 4){
            this.X = 16;
            this.Y = 16;
            //System.out.println("" + X + "," + Y);
            image = new ImageIcon("H:\\My Pictures\\BomberMan\\BMBlue.png");
        }
    Score = 0;
    Lives = 3;
}

Right now my code is displaying:

0,0

16,0

0,16

16,16

when it should be displaying:

0,0

0,0

0,0

0,0

because the P1.X and P1.Y are initialized as 0 and 0 and are not supposed to be changed in my code. I have no idea why it is changing the P1.X and P1.Y values when I am not touching them at all. Can someone please explain this to me? Note: I have tried creating a separate method to set the information and an array of PlayerInfo's, but nothing works. Thanks in advance.

share|improve this question

1 Answer

In all the if clauses of PlayerInfo class you are assigning values to X and Y. For example:

 this.X = 0;
 this.Y = 0;

That is the reason why the values are changing because the X and Y variables are static. Remove the static for both the variables in PlayerInfo and then you will get the behaviour you want.

static variables are shared across different objects of the class. So if you assigned P1.X = 10, then P2.X, P3.X, P4.X, etc will also become 10 as the same variable is shared by all the object.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.