-1

I have two classes, one is House, which surely has other attributes, but I have included only the one with which I have had problem:

public class House {

private Address addressOfHouse;

public House (Address addressOfHouse){
    this.setAddressOfHouse(addressOfHouse);

public Address getAddressOfHouse() {
    return addressOfHouse;
}

public void setAddressOfHouse(Address addressOfHouse) {
    this.addressOfHouse = addressOfHouse;
}
}

The other is Address:

public class Address {

public String streetName;
public String city;
public int postalCode;

public Address (String streetName, String city, int postalCode){
    this.streetName=streetName;
    this.city=city;
    this.postalCode=postalCode;
}

public String getStreetName() {
    return streetName;
}

public String getCity() {
    return city;
}

public int getPostalCode() {
    return postalCode;
}
}

When I create an object "house", set an address to it and try to call the method "getAddressofHouse":

public class Main {

public static void main(String[] args) {
    Address address2 = new Address("Y", "Praha", 11000);

    House house2 = new House (address2);

    System.out.println(house2.getAddressOfHouse());

It does not give me the address of the house. It gives me this:

com.company.Address@7f63425a

My problem is to know what I have done wrongly with my setter and getter of the address in the class "House". It is an exercise that I have been doing, and it asks me to have an attribute called "addressOfHouse" with with data type "Address" (the class "Address"). So I suppose that I have to call the address of the house through the method "getAddressOfHouse", but I have been trying it and it has not worked at all. Could you help me with it? If you think that I should put the whole code here, I will do it. I thank you all in advance.

2 Answers2

4

System.out.println(house2.getAddressOfHouse());

Output

com.company.Address@7f63425a

This is default implementation of Object.toString().

If you want to see smth more usave, you have to override public String toString() { } method in your Adress class. E.g.:

public String toString() {
    return "Address{" +
            "streetName='" + streetName + '\'' +
            ", city='" + city + '\'' +
            ", postalCode=" + postalCode +
            '}';
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
1

You need to add a toString() method to the address. If not it will print the reference of the object as the default when you try to print.

See: toString() doc. Which I repeat here:

public String toString() Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())
Returns: a string representation of the object.

Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79