StackTips
 6 minutes

Java Method Chaining with Example

By Editorial @stacktips, On Sep 17, 2023 Java 2.57K Views

The term method chaining refers to both a design and a convention. Each method returns an object, allowing the calls to be chained together in a single statement. Chaining is syntactic sugar which eliminates the need for intermediate variables.

A method chain is also known as a train wreck due to the increase in the number of methods that come one after another in the same line that occurs as more methods are chained together even though line breaks are often added between methods.

It applies to classes and methods where:

  • Multiple methods are potentially going to be called on the same object;
  • The methods in question need no return value.

The idea of method chaining is that if the methods need no “useful” return value, then the method can return this

Method Chaining:

class User {

    private String name;
    private int age;

    // In addition to having the side-effect of setting the attributes in question,
    // the setters return "this" (the current Person object) to allow for further chained method calls.
    public User setName(String name) {
        this.name = name;
        return this;
    }
     
    public User setAge(int age) {
        this.age = age;
        return this;
    }
    
    public void getUserDetails() {
        System.out.println("User name is " + name + " and " + age + " years old.");
    }
    
    public static void main(String[] args) {
         User user= new User(); 
         user.setName("skptricks").setAge(22).getUserDetails();
    }
}

Output : Username is skptricks and 22 years old.

Without Method Chaining:

class User {
    
    private String name;
    private int age;

    // In addition to having the side-effect of setting the attributes in question,
    // the setters return "this" (the current Person object) to allow for further chained method calls.
    public User setName(String name) {
        this.name = name;
        return this;
    }
     
    public User setAge(int age) {
        this.age = age;
        return this;
    }
    
    public void getUserDetails() {
        System.out.println("User name is " + name + " and " + age + " years old.");
    }
    
    public static void main(String[] args) {
         User user= new User(); 
         user.setName("skptricks").setAge(22).getUserDetails();
    }
}

Output : Username is skptricks and 22 years old.

Hope you like this simple example of method chaining. Thank you for reading this article, and if you have any problem, have another better useful solution for this article, please write in the comment section.

This story was first published at SKPTRICKS. See the original article Method chaining in Java. Opinions expressed by Stacktips contributors are their own.

stacktips avtar

Editorial

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages.