Mackenzie
Mackenzie

Reputation: 83

All the objects I instantiate of one of my classes print with the same values as the last object of that type instantiated

I cannot understand it, I declare/instantiate all the objects with separate names and values, like so:

// Initialise cars - null constructors and non-null constructors.
    ParkedCar car1 = new ParkedCar();
    ParkedCar car2 = new ParkedCar("BBB 222", "Toyota", "Echo", "Black", 200);
    ParkedCar car3 = new ParkedCar("CCC 333", "Ford", "Taurus", "Silver", 100);
    ParkedCar car4 = new ParkedCar("DDD 444", "Dodge", "Charger", "Blue", 500);
    ParkedCar car5 = new ParkedCar("EEE 555", "Kia", "Rio", "Grey", 75);
//Initialise meters
    ParkingMeter pm1 = new ParkingMeter(car1, 60);
    ParkingMeter pm2 = new ParkingMeter(car2, 60);
    ParkingMeter pm3 = new ParkingMeter(car3, 120);
    ParkingMeter pm4 = new ParkingMeter(car4, 0);
    ParkingMeter pm5 = new ParkingMeter(car5, 60);

(here's the constructors from the other class in case you're curious:

public ParkedCar()
{
    licensePlate = "AAA 111";
    carMake = "Honda";
    carModel = "Accord";
    carColour = "Red";
    minutesParked = 15;
}

public ParkedCar(String license, String carMa, String carMod, String carCol, int time)
{
    carMake = carMa;
    carModel = carMod;
    carColour = carCol;
    licensePlate = license;
    minutesParked = time;
}

So the objective is to have ParkedCar objects that have a ParkingMeter object for each (just sets the timePaid variable on the ParkedCar object), along with a PoliceOfficer object to check the "time paid" vs "time parked" and issue a Parking Ticket (instantiate a ParkingTicket object) with a fine that corresponds to how long they've been parked overdue. I can provide the full classes, but here's the relevant parts of the ParkingTicket class, since that's where it is printing from:

public ParkingTicket(ParkedCar car)
{
    ParkedCar illegalParker = car;
    licensePlate = illegalParker.getLicensePlate();
    carMake = illegalParker.getCarMake();
    carModel = illegalParker.getCarModel();
    carColour = illegalParker.getCarColour();
    minutesParked = illegalParker.getMinutesParked();
    minutesPaid = illegalParker.getMinutesPaid();
    fineAmount = ParkingTicket.getFineAmount(illegalParker);
    badgeNumber = PoliceOfficer.getBadgeNumber();
    officerName = PoliceOfficer.getOfficerName();
}
public static String getTicketText(ParkedCar car, PoliceOfficer issuer)
{
    PoliceOfficer ticketIssuer = issuer;
    ParkedCar illegalParker = car;
    ticketText = "PARKING TICKET $"+ParkingTicket.getFineAmount(illegalParker)+".\n"
        +"License Plate: "+illegalParker.getLicensePlate()+".\n"
        +"Car Description: "+illegalParker.getCarColour()+" "+illegalParker.getCarMake()+" "+illegalParker.getCarModel()+".\n"
        +"Issued by: "+ticketIssuer.getOfficerName()+", Badge Number: "+ticketIssuer.getBadgeNumber()+".\n";
    return ticketText;
}
public String toString()
{
    return "PARKING TICKET $"+this.fineAmount+".\n"
        +"License Plate: "+this.licensePlate+".\n"
        +"Car Description: "+this.carColour+" "+this.carMake+" "+this.carModel+".\n"
        +"Issued by: "+this.officerName+", Badge Number: "+this.badgeNumber+".\n";
}

It doesn't matter whether I use the .toString() method by printing the object directly or by printing the String returned by the .getTicketText() method...

Why are all the ParkedCar objects' values being set to car5's values??

Feel free to ask for more snippets of code, since the point of this assignment is to demonstrate connectivity between classes and a lot of referencing back and forth is going on.

Upvotes: 0

Views: 113

Answers (2)

Elliott Frisch
Elliott Frisch

Reputation: 201437

static fields are shared by all instances. Remove the static

private String carMake; // <-- not static
private String carModel; // <-- not static
private String carColour; // <-- not static
private String licensePlate; // <-- not static
private int minutesParked; // <-- not static

So that each instance gets its' own.

JLS-8.3.1.1. static Fields says (in part),

If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized (§12.4).

A field that is not declared static (sometimes called a non-static field) is called an instance variable. Whenever a new instance of a class is created (§12.5), a new variable associated with that instance is created for every instance variable declared in that class or any of its superclasses.

Upvotes: 5

mattsap
mattsap

Reputation: 3806

The problem is that your methods are static. Basically, when a method or variable is static, it is shared across all instantiations of the class. Because of this, every call to your static function will be the same. I think you may want to consider whether the functions you mentioned should be static or not.

Upvotes: 0

Related Questions