Introduction to method chaining in Java

Objective: This segment introduces how to use method chaining in Java with examples.
Method chaining is a technique in object-oriented programming to invoke multiple methods using a single object. Here, each method returns an object, and we can call methods in a single statement using the dot(.) operator. The advantage of method chaining is that it improves the readability and less code required.

Implementation of Java method chaining
KW.java
class holders
{
    long haccno;
    String hname,hcity;
    public holders account_no(long haccno)
    {
        this.haccno=haccno;
        return this;
    }
    public holders name(String hname)
    {
        this.hname=hname;
        return this;
    }
    public holders city(String hcity)
    {
        this.hcity=hcity;
        return this;
    }
    public void showrecords()
    {
        System.out.println(haccno+" | "+hname+" | "+hcity);
    }
}
class KW
{
    public static void main(String[] args) 
    {
        holders insert=new holders();
        insert.account_no(25622348989L).name("James Moore").city("Phoenix").showrecords();
        insert.account_no(25622348992L).name("Ryan Bakshi").city("Mumbai").showrecords();
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW 25622348989 | James Moore | Phoenix 25622348992 | Ryan Bakshi | Mumbai kodingwindow@kw:~$
Implementation without method chaining
KW.java
class holders
{
    long haccno;
    String hname,hcity;
    public void account_no(long haccno)
    {
        this.haccno=haccno;
    }
    public void name(String hname)
    {
        this.hname=hname;
    }
    public void city(String hcity)
    {
        this.hcity=hcity;
    }
    public void showrecords()
    {
        System.out.println(haccno+" | "+hname+" | "+hcity);
    }
}
class KW
{
    public static void main(String[] args) 
    {
        holders insert=new holders();
        insert.account_no(25622348989L);
        insert.name("James Moore");
        insert.city("Phoenix");
        insert.showrecords();
        
        insert.account_no(25622348992L);
        insert.name("Ryan Bakshi");
        insert.city("Mumbai");
        insert.showrecords();
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW 25622348989 | James Moore | Phoenix 25622348992 | Ryan Bakshi | Mumbai kodingwindow@kw:~$
Advertisement