0

I am a beginner with java, and I am confused on why my code only returns the values of the most recent object. I am creating a bunch of properties for my Monopoly project inside a class Property using a parameterized constructor.

For the Property class, I made these variables and constructors:

public class Property {

    private static String name;
    private static int owner;
    private static int price;
    private static int mortgage;
    private static boolean mortgaged;
    private static int houses;
    private static int set;
    private static int rent;
    private static int houseOneRent;
    private static int houseTwoRent;
    private static int houseThreeRent;
    private static int houseFourRent;
    private static int hotelRent;
    private static int housePrice;
    
    
    // Constructor
    
    public Property(String name, int price, int rent, int house1, int house2, int house3, int house4, int hotel, int housePrice) {
        
        this.name = name;
        owner = 0;
        this.price = price;
        this.mortgage = price/2;
        mortgaged = false;
        houses = 0;
        set = 0;
        this.rent = rent;
        this.houseOneRent = house1;
        this.houseTwoRent = house2;
        this.houseThreeRent = house3;
        this.houseFourRent = house4;
        this.hotelRent = hotel;
        this.housePrice = housePrice;
        
        System.out.println("The " + this.name + " property has been initialized.");
    }

I then created all my properties in a different class, using code like this. I did it for all the properties but I won't show them to save space.

Property Yellow1 = new Property("Atlantic Avenue", 260, 22, 110, 330, 800, 975, 1150, 150);
        Property Yellow2 = new Property("Ventnor Avenue", 260, 22, 110, 330, 800, 975, 1150, 150);
        Property Yellow3 = new Property("Marvin Gardens", 280, 24, 120, 360, 850, 1025, 1200, 150);
        
        Property Green1 = new Property("Pacific Avenue", 300, 26, 130, 390, 900, 1100, 1275, 200);
        Property Green2 = new Property("North Caroline Avenue", 300, 26, 130, 390, 900, 1100, 1275, 200);
        Property Green3 = new Property("Pennsylvania Avenue", 320, 38, 150, 450, 1000, 1200, 1400, 200);
        
        Property DarkBlue1 = new Property("Park Place", 350, 35, 175, 500, 1100, 1300, 1500, 200);
        Property DarkBlue2 = new Property("Broadway", 400, 50, 200, 600, 1400, 1700, 2000, 200);

I then created getValue methods such as these below:

public static String getName() {
        
        return name;
    }
    
    public static int getOwner() {
        
        return owner;
    }
    
    public static int getPrice() {
        
        return price;
    }

When I ran the getValue methods like this:

System.out.println(Yellow1.getHousePrice());

It output:

200

200 is the correct value for the final object I constructed; DarkBlue1. The value that should have been output for Yellow1 is 150. Why is it outputting 200?

  • 5
    Your properties should not be `static`. – Marvin Dec 20 '22 at 10:21
  • `static` means that is only one instance of each of those fields no matter how many Property instances you create. You actually want a separate instance for each Property - remove `static` from the fields and the methods. – greg-449 Dec 20 '22 at 10:43

0 Answers0