user2985842
user2985842

Reputation: 457

Methods with generic type parameter can pass generic type parameters

I have instance with getters and setters in my Customer Class

private Collection<CustomersDetails> customerDetails = new ArrayList<CustomersDetails>();

    public Collection<CustomersDetails> getCustomerDetails() {
        return customerDetails;
    }
    public void setCustomerDetails(Collection<CustomersDetails> customerDetails) {
        this.customerDetails = customerDetails;
    }

but when i am trying to pass

CustomersDetails customersDetails = new CustomersDetails();

customersDetails.set....
customersDetails.set....


Customer customer = new Customer();
customer.setCustomerDetails(customersDetails);

its giving me error

The method setCustomerDetails(Collection<CustomersDetails>) in the type Customer is not applicable for the arguments (CustomersDetails) 

Why its so , when my collection is of CustomersDetails type??

Upvotes: 0

Views: 48

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279890

Your Customer class has a method

public void setCustomerDetails(Collection<CustomersDetails> customerDetails) {
    this.customerDetails = customerDetails;
}

You are trying to invoke it with an argument of type CustomersDetails. A CustomersDetails is not a Collection<CustomersDetails>. Wrap the element in a List or other Collection.

Something like

customer.setCustomerDetails(Arrays.asList(customersDetails));

Upvotes: 3

Related Questions