I have two classes: Apartments and Building. The Apartment class:
public class Apartment {
private String familyName;
private int rent;
private boolean isRented;
}
The Building class has an Apartment array, and a numOfApartments variable which indicates how many apartments the building has, and also a FINAL variable who indicates the max number for apartments in one building.
private Apartment[] apartments;
private int numOfApartments;
private final int MAX_APARTMENTS=20;
public Building() {
this.apartments=new Apartment[MAX_APARTMENTS];
this.numOfApartments=0;
}
Now I want to make a method which raises the rent for an apartment by an amount which is taken as parameter:
public void raiseRent(int amount) {
for (int i=0; i<numOfApartments; i++) {
apartments[i].setRent(apartments[i].getRent() + amount);
}
}
the addAppartment method:
public boolean addApartment(Apartment a){ //adds an appartment to the building if
//there's enough space in the array.
if (this.numOfApartments<this.MAX_APARTMENTS)
{
this.apartments[this.numOfApartments]=new Apartment(a);
this.numOfApartments++;
return true;
}
return false;
}
The apartment class string ToString method:
public String toString()
{
if (this.isRented) //if the apartment is rented, prints:
{
return "Apartment rent: " + this.rent + ". rented to " + this.familyName + " family.";
}
else
return "Apartment rent: " + this.rent + ". not rented yet.";
}
The main is:
String familyName; int rent;
System.out.println("enter family name and rent by this order");
familyName=scan.next();
rent=scan.nextInt();
Apartment a=new Apartment(familyName,rent); //build new apartment object.
Building b=new Building();
System.out.println(b.addApartment(a));
b.raiseRent(200); //initiate the raiseRent method.
Everything here works, but when I write this:
System.out.println(a);
The rent is shown as the old rent, not the updated one.
When I write this:
System.out.println(b.apartments[0]);
It works.
Why when I write System.out.println(a); it doesn’t work?
Thank you for the answer.
In your
public boolean addApartment(Apartment a)method:Creates a new apartment which appears to be constructed from an existing apartment. This is a copy.
After you raise the rent, the
Apartment a=new Apartment(familyName,rent); //build new apartment object.That you passed in to
b.addApartment(a)Will not have an updated rent. This is because Java is pass-by-value. It’s a “new”
intin thenew Apartmentyour BuildingApartment[]collection contains, even if you set it from an existing one (this.rent = other.rent)