In Java you have to write a lot of additional and repetitive code along with your own code. This additional - unfortunately mandatory - code is eating up space in your preferred IDE and also eating up your time. In this article we will dig deeper into the topic.

What does boilerplate mean?

The name boilerplate comes from the newspaper printing industry and was used for any repetitive text, which was used in various contexts and pages and was mandatory for the newspapers to use it. So this repetitive text, mostly used as a filler for the newspaper, has been stamped in special metal plates for the printing process. Since the plates looked like plates on boilers, they quickly got the name boilerplates and the text got the name boilerplate text, or even shorter, boilerplate.

But what is the meaning of boilerplate in the programming context?

Let's say you want to define a simple model class with the following class fields:

  • boolean field someBoolean
  • String field someStringField
  • float field someFloatField

This requirement seems pretty simple and the code could look something like this (unfortunately that is not the case):

/**
* my model class with fields.
/
public class MyModelClass {
private boolean someBoolean;
private String someStringField;
private float someExcludedField;
}

To make this class usable and runnable you would need to add the following methods:

  • a no argument constructor
  • an all field as argument constructor
  • getters for all fields
  • setters for all fields
  • a human usable toString method
  • an equals method to fulfill the object identity requirements in Java
  • a hashCode method to fulfill the object identity requirements in Java

With these requirements your code is getting extended by a lot of boilerplate code.

/**
* my model class with fields.
*
*/
public class MyModelClass {
private boolean someBoolean;
private String someStringField;
private float someExcludedField;

public MyModelClass() {
}

public MyModelClass(boolean someBoolean, String someStringField, float someExcludedField) {
super();
this.someBoolean = someBoolean;
this.someStringField = someStringField;
this.someExcludedField = someExcludedField;
}

@Override
public String toString() {
return "MyModelClass [someBoolean=" + someBoolean + ", someStringField=" + someStringField + ", someExcludedField=" + someExcludedField + "]";
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (someBoolean ? 1231 : 1237);
result = prime * result + Float.floatToIntBits(someExcludedField);
result = prime * result + ((someStringField == null) ? 0 : someStringField.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyModelClass other = (MyModelClass) obj;
if (someBoolean != other.someBoolean)
return false;
if (Float.floatToIntBits(someExcludedField) != Float.floatToIntBits(other.someExcludedField))
return false;
if (someStringField == null) {
if (other.someStringField != null)
return false;
} else if (!someStringField.equals(other.someStringField))
return false;
return true;
}

public boolean getSomeBoolean() {
return someBoolean;
}

public void setSomeBoolean(boolean someBoolean) {
this.someBoolean = someBoolean;
}

public String getSomeStringField() {
return someStringField;
}

public void setSomeStringField(String someStringField) {
this.someStringField = someStringField;
}

public float getSomeExcludedField() {
return someExcludedField;
}

public void setSomeExcludedField(float someExcludedField) {
this.someExcludedField = someExcludedField;
}
}

You can see that the main interesting code is getting inflated with a lot of boilerplate code, which is needed in Java.

If you want to learn how to get rid of all this boilerplate code in Java read the article about How to Get Rid of Boilerplate Code with Lombok.