Change field by creating new object

When i create ship, its fields: height=50 and width=29 as i want. But after i create block it changes ship's fields (height and width) into 25. Can you tell me why?

Examples of code:

class Unit:

package test;


import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Unit {
    private int x,y;
    private String imageFileName;
    private BufferedImage img;

    private int a,b,c,d;

    private static int height;

    private static int width;

    public Unit(String imageFileName, int width, int height, int x, int y) {
        this.x=x;
        this.y=y;
        this.width=width;
        this.height=height;
        a=y;
        b=x+width;
        c=y+height;
        d=x;
    }

    public int getX() {
        return x;
    }
    int getY() {
        return y;
    }
    public void setY(int i) {
        y=i;
        setEdges();
    }
    public void setX(int i) {
        x=i;
        setEdges();
    }
    public void setEdges() {
        a=y;
        b=x+width;
        c=y+height;
        d=x;
    }
    public void showUnit() {
        System.out.println("x = " + x + " y= " + y);
        System.out.println("a = " + a + " b= " + b);
        System.out.println("c = " + c + " d= " + d);
        System.out.println("height = " + height + " width= " + width);
    }

}

class Block:

package test;


import java.awt.Image;

public class Block extends Unit {
    public static int BLOCK_SIZE=25;
    public Block(int number, int x, int y) {    
        super("kafelki/"+Integer.toString(number)+".png", BLOCK_SIZE, BLOCK_SIZE, x, y);
    }   
    public int getX() {
        return super.getX();
    }

    public int getY() {
        return super.getY();
    }
    public void show() {
        super.showUnit();
    }
}

class Ship:

package test;


import static java.lang.Math.abs;

import java.awt.Image;

public class Ship extends Unit {
    public static int HEIGHT_C=50;
    public static int WIDTH_C=29;
    private double vUpDown;
    private double vLeftRight;
    public Ship(int x, int y) {
        super("1", WIDTH_C, HEIGHT_C, x, y);
    }
    public int getX() {
        return super.getX();
    }
    public int getY() {
        return super.getY();
    }
    public void setY(int i) {
        super.setY(i);
    }

    public void setX(int i) {
        super.setX(i);
    }

    public void showShip() {
        System.out.println("####SHIP####");
        super.showUnit();
        System.out.println("Vupdown = " + vUpDown + " Vleftright= " + vLeftRight);
        System.out.println("############");
    }

} 

class Test:

public class Test {
    public static void main(String[] args) {
        Ship ship = new Ship(100,100);
        ship.showShip();
        Block block = new Block (1, 200, 200);
        ship.showShip();
    }
}
Jon Skeet
people
quotationmark

This is the problem:

private static int height;
private static int width;

Those fields are static, meaning they're set for the type itself rather than being specific to any instance of the type.

I strongly suspect they should just be instance fields instead:

private int height;
private int width;

As a general rule, if you're setting static fields in a constructor, that should be a warning sign.

people

See more on this question at Stackoverflow