J
J
Juris2020-03-24 13:11:27
Java
Juris, 2020-03-24 13:11:27

Problem with object initialization (constructor), what am I doing wrong?

Good day!
I recently started learning Java and stumbled over constructors.

package com.company.personidentity;

public class Main {
    public static void main(String[] args) {
       Person person1 = new Person("Harry Potter", 40, "Baker street 20","Manchester","United Kingdom");
    }
}


Intellij IDEA is already suggesting that there is a type mismatch, I have an Address type, and I kind of slip in a String. How to solve this problem? I want the address to be a separate class
package com.company.personidentity;

public class Person {
    private String name;
    private int age;
    private Address address;
    private Address city;
    private Address country;

    public Person(String name, int age, Address address, Address city, Address country) {
        this.name = name;
        this.age = age;
        this.address = address;
        this.city = city;
        this.country = country;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                ", city=" + city +
                ", country=" + country +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Address getCity() {
        return city;
    }

    public void setCity(Address city) {
        this.city = city;
    }

    public Address getCountry() {
        return country;
    }

    public void setCountry(Address country) {
        this.country = country;
    }
}

package com.company.personidentity;

public class Person {
    private String name;
    private int age;
    private Address address;
    private Address city;
    private Address country;

    public Person(String name, int age, Address address, Address city, Address country) {
        this.name = name;
        this.age = age;
        this.address = address;
        this.city = city;
        this.country = country;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                ", city=" + city +
                ", country=" + country +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Address getCity() {
        return city;
    }

    public void setCity(Address city) {
        this.city = city;
    }

    public Address getCountry() {
        return country;
    }

    public void setCountry(Address country) {
        this.country = country;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2020-03-24
@Balticman

like as I palm off String. How to solve this problem? I want the address to be a separate class
Well, make a separate class and pass it, what's the problem?
new Person("Harry Potter", 40, new Address("Baker street 20"), new Address("Manchester"), new Address("United Kingdom"));

Another question is why do you have street, city and country of the same type...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question