How to do method chaining in Java? o.m1().m2().m3().m4()

This pattern is called “Fluent Interfaces” (see Wikipedia)

Just return this; from the methods instead of returning nothing.

So for example

public void makeText(String text) {
    this.text = text;
}

would become

public Toast makeText(String text) {
    this.text = text;
    return this;
}

Leave a Comment