Tutorials Java Java Method Chaining with Example

Java Method Chaining with Example

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.

Total
0
Shares
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Chittaranjan Ghosh
Chittaranjan Ghosh
3 years ago

you have a little problem in this webpage. The page title starts with “ava”, should be java, I guess. Could not help but notice this as I am a QA.

Previous Post

Popular Java Object Mapping Frameworks

Next Post

Create and Manage Git Branches from Command Line

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
1
0
Would love your thoughts, please comment.x
()
x