Home > Enterprise >  How to instantiate an instance variable using constructor when a parameter of this constructor is an
How to instantiate an instance variable using constructor when a parameter of this constructor is an

Time:05-09

I created the class Person and the class Address. As i'm trying to Instantiate an instance variable p1 in the class MyApp i get an error 'Address(java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)' in Address' cannot be applied to '(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)'

public class Person {
    private String firstname;
    private String lastname;
    private Address address;
    private String dateOfBirth;
    private String[] hobbies;

    public Person(String firstname, String lastname, Address address, String dateOfBirth, String[] hobbies) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.address = address;
        this.dateOfBirth = dateOfBirth;
        this.hobbies = hobbies;
    }
}


public class Address {
    private int houseNumber;
    private String street;  
    private String city;

    public Address( int housenum,String street, String city) {
        this.houseNumber = housenum;
        this.street = street;
        this.city = city;
    }
}

public class myApp{
    public static void main(String[] args) {
        Person p1= new Person("Rares","Maria", new Address(84,"hiw RD","London"),csv_row1[2],"10/04/2000","swimming");

    }
}

CodePudding user response:

3 Things:

1.) Don't begin a class name lowercase cause it is a type and types start always upcase by convention.

2.) what is csv_row[2] it is not defined in your code and there is also no field in the constructor that matches.

3.) In Person the last member is String array and not string.

Change your code to of myApp to:

public class myApp {
    public static void main(String[] args) {
        Person p1= new Person("Rares","Maria", new Address(84,"hiw RD","London"),"10/04/2000", new String[]{"swimming"});

        System.out.println("p1:"   p1);
    }
}

So you tried to call a constructor you have not defined. You also can make passing a sting array as last parameter more convenient:

public class Person {
    private String firstname;
    private String lastname;
    private Address address;
    private String dateOfBirth;
    private String[] hobbies;

    public Person(String firstname, String lastname, Address address, String dateOfBirth, String ... hobbies) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.address = address;
        this.dateOfBirth = dateOfBirth;
        this.hobbies = hobbies;
    }
}

then this constructor call would be correct:

public class myApp {
    public static void main(String[] args) {
        Person p1= new Person("Rares","Maria", new Address(84,"hiw RD","London"),"10/04/2000", "swimming");

        System.out.println("p1:"   p1);
    }
}

The error message itself makes no sense to me. Did you clean before compilation? It looks like outdated .class-Files. Remove all .class-Files and re-compile.

Hope this helps.

CodePudding user response:

I think that you're giving wrong parameters for the Address constructor :

Address (String, String, int, String, String);

While you have defined it as follows :

Address ( String, String, String, String);

But your your code is correct, so it's not making any sense.
Something else, what is csv_row1[2] for ? It's not defined, remove it or define it in your code
Something else, The hobbies parameter is defined as String[] not String and you've to match it.
So your person instancing would be :

String[] hobbies = {"swimming"};
Person p1= new Person("Rares","Maria", new Address(84,"hiw RD","London"),"10/04/2000", hobbies);

Or

Person p1= new Person("Rares","Maria", new Address(84,"hiw RD","London"),"10/04/2000", new String[]{"swimming"});
  • Related