Method Chaining in Java

Sheeraz Gul Oct 12, 2023
Method Chaining in Java

This tutorial demonstrates how to perform method chaining in Java.

Method Chaining in Java

Method chaining calls multiple methods simultaneously using the dot . operators. Method chaining calls multiple methods in one statement, whereas a chain of methods is called in one statement.

Method chaining is a concept from Object Oriented Program where each method in a chain will return an object. Method chaining can also be defined as calling multiple methods on one object.

The syntax for method chaining is:

DemoObject.method1().method2().method3();

As we can see in the syntax, three methods are called on the DemoObject. Calling one method after another like this is called the concept of method chaining.

The method chaining is also known as the Parameter Idiom in Java. Sometimes, we can call it a train wreck when the number of methods increases.

Let’s try an example of method chaining in Java:

package delftstack;
class Demo {
  private int DemoInt;
  private float DemoFloat;

  Demo() {
    System.out.println("This is the consutructor");
  }

  int SetInteger(int DemoInt) {
    this.DemoInt = DemoInt;
    return this.DemoInt;
  }

  float SetFloat(float DemoFloat) {
    this.DemoFloat = DemoFloat;
    return this.DemoFloat;
  }

  void DisplayData() {
    System.out.println("Display Numbers=" + DemoInt + " " + DemoFloat);
  }
}

public class Example {
  public static void main(String[] args) {
    new Demo().SetInteger(34).DisplayData();
  }
}

The code above applied the method chaining on the object of the Demo class. But it will throw an error because the DisplayData method returns an object where the SetInteger is returning an int value.

The method chaining concept doesn’t work in that way.

See the output:

public class Example {
       ^
Demo.java:33: error: int cannot be dereferenced
new Demo().SetInteger(34).DisplayData();
^
2 errors

To solve this issue, we have to define the method as a type of Demo class and return only the this object in the methods.

See the example:

package delftstack;

class Demo {
  private int DemoInteger;
  private float DemoFloat;

  Demo() {
    System.out.println("This is the consutructor");
  }

  public Demo SetInteger(int DemoInteger) {
    this.DemoInteger = DemoInteger;
    return this;
  }

  public Demo SetFloat(float DemoFloat) {
    this.DemoFloat = DemoFloat;
    return this;
  }

  void DisplayData() {
    System.out.println("Display Numbers=" + DemoInteger + " " + DemoFloat);
  }
}

class Example {
  public static void main(String[] args) {
    new Demo().SetInteger(43).SetFloat(23).DisplayData();
  }
}

Now the code will work properly. See the output:

This is the consutructor
Display Numbers=43 23.0

Here are some important points to consider when working with method chaining in Java:

  1. Whenever the constructor is called, we should ensure the constructor doesn’t hold any return value.
  2. The constructor always returns an object reference, so we can use this object reference to call another method.
  3. While applying method chaining with a dot . operator, we should ensure that each method in the chain returns an object reference.

For example, in the first example, the method SetInteger returns an int, and the DisplayData method returns an object reference, which is why it returns an error. Similarly, in the second example, every method returns an object reference, which is why it’s working correctly.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Method