Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools

Can you explain the constructor of ContactBuilder and Contact declared... please to get clear understanding.

How can you use this as a return type

package com.teamtreehouse.contactmgr.model;

import javax.persistence.*;

/**
 * Created by Nirbhay's Workstatio on 10-02-2017.
 */
@Entity
public class Contact {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @Column
    private String firstName;
    @Column
    private String lastName;
    @Column
    private String email;
    @Column
    private Long phone;

    //Default constructor for JPA
    public Contact() {}

    public Contact(ContactBuilder builder) {
        this.firstName = builder.firstName;
        this.lastName = builder.lastName;
        this.email = builder.email;
        this.phone = builder.phone;
    }

    @Override
    public String toString() {
        return "Contact{" +
                "id=" + id +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                ", phone=" + phone +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Long getPhone() {
        return phone;
    }

    public void setPhone(Long phone) {
        this.phone = phone;
    }
    public static class ContactBuilder {
        private String firstName;
        private String lastName;
        private String email;
        private Long phone;
    }
        public ContactBuilder(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
        }
        public ContactBuilder withEmail(String email) {
        this.email = email;
        return this;
        }
        public ContactBuilder withPhone(String phone) {
        this.phone = phone;
        return this;
        }
        public Contact build() {
        return new Contact(this);
        }

}