Search code examples
javaclassbuilder

Problem writing a builder for a class in java


I have problem writing a builder for a class in java. It always gives me an error saying could not find symbol.

class Company {
    private String name;
    private String address;
    private String contact;
    private int sizeOfEmployee;
    private Date createdTime;
  
    @Override
    public String toString() {
        return "Company{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", contact='" + contact + '\'' +
                ", sizeOfEmployee=" + sizeOfEmployee +
                ", createdTime=" + createdTime +
                '}';
    }
   
   public static class builder {
    private String name;
    private String address;
    private String contact;
    private int sizeOfEmployee;
    private Date createdTime;
    
    
   public builder Name (String name){
     this.name = name;
     return this;
   }
    public builder address(String address){
     this.address = address;
     return this;
   }
   
    public builder contact(String contact){
     this.contact = contact;
     return this;
   }
    public builder size(int sizeOfEmployee){
     this.sizeOfEmployee = sizeOfEmployee;
     return this;
   }
    public builder date(Date createdTime){
     this.createdTime = createdTime;
     return this;
   }
   
   }
   
     
}

I would like to write a builder that could print out my Company class. Could anyone tell me why my builder is not working? I search for how to write a builder and try to copy the method. But it's not working here. It gives me an error saying error can not find symbol. If I call

Company abc = Company.builder().name("abc").address("Virginia").createdTime(new Date()).sizeOfEmployee(300).contact("12345").build();

It will print out

Company{name='abc', address='Virginia', contact='12345', sizeOfEmployee=300, createdTime=Wed Oct 21 11:50:41 EDT 2020}

Solution

  • You are calling static method builder 'Company.builder()' which does not exists. You can solve the problem this way:

    public class Company {
        private String name;
        private String address;
    
        public static CompanyBuilder builder() {
            return new CompanyBuilder();
        }
        private static class CompanyBuilder {
            private final Company company = new Company();
            public CompanyBuilder name(String name){
                company.name = name;
                return this;
            }
            public CompanyBuilder address(String address){
                company.address = address;
                return this;
            }
            public Company build() {
                return company;
            }
        }
        public static void main(String[] args) {
            System.out.println(Company.builder().name("name").address("address").build());
        }
    }
    

    I would suggest Lombok for java projects. With one annotation @Builder including the Lombok plugin and dependency you can solve the problem, also you can use a lot of features instead of boilerplate code.

    import lombok.Builder;
    import lombok.ToString;
    
    @Builder
    @ToString
    public class Company {
        private String name;
        private String address;
    
        public static void main(String[] args) {
            System.out.println(Company.builder().name("name").address("address").build());
        }
    }