0

So I've seen, in many places, calling methods of a class like:

SomeClass obj = new SomeClass();
obj.addX(3).addY(4).setSomething("something").execute();

I don't think I completely understand how that works. Is each method independent of each other, so the above is equal to:

obj.addX(3);
obj.addY(4);
obj.addSomething("something");
obj.execute();

Or are they designing their class structure in some other fashion that allows for this. If they are how are they designing their classes to support this?

Also, does that have a specific name? Or is this just calling methods on a class?

3
  • 1
    @dlev - Fluent interfaces are more general than this, I think; the better term would be method chaining (or method cascading). Commented Jun 24, 2014 at 20:15
  • @TedHopp Fair point, though in the vast majority of cases I've seen, method chaining is primarily used in support of fluent interfaces. Nonetheless I'll remove my erroneous comment :) Commented Jun 24, 2014 at 20:17
  • Each method has return this; Commented Jun 24, 2014 at 20:29

4 Answers 4

7

That would be method chaining. It can do one of two things.

  1. Each call to a method returns this which allows you to continue to call methods on the original instance.

    public class SomeClass
    {
        private int _x = 0;
        private int _y = 0;
        private String _something = "";
    
        public SomeClass addX(int n)
        {
            _x += n;
            return this;
        }
    
        public SomeClass addY(int n)
        {
            _y += n;
            return this;
        }
    
        public SomeClass setSomething(String something)
        {
            _something = something;
            return this;
        }
    
        // And so on, and so on, and so on...
    }
    
  2. Each method call returns a new instance of the class with everything copied/updated appropriately. This makes the class immutable (so you don't accidentally modify something that you didn't mean to).

    public class SomeClass
    {
        private int _x = 0;
        private int _y = 0;
        private String _something = "";
    
        public SomeClass(int x, int y, String something)
        {
            _x = x;
            _y = y;
            _something = something;
        }
    
        public SomeClass addX(int n)
        {
            return new SomeClass(_x + n, _y, _something);
        }
    
        public SomeClass addY(int n)
        {
            return new SomeClass(_x, _y + n, _something);
        }
    
        public SomeClass setSomething(String something)
        {
            return new SomeClass(_x, _y, something);
        }
    
        // And so on, and so on, and so on...
    }
    

Some people have also mentioned Fluent Interfaces. Fluent Interfaces utilize method chaining to create an API that provides something along the lines of a Domain Specific Language which can make code read much more clearly. In this case, your example doesn't quite qualify.

Sign up to request clarification or add additional context in comments.

Comments

2

they modify object's state and return the same object back mostly

class Number{
 int num;

 public Number add(int number){
     num+=number;
     return this;
 }

}

you can call it like

new Number().add(1).add(2);

most of the time the use case is to return new Object to support immutability

2 Comments

Note that it is not necessarily the case that the same object is returned. It's completely plausible that a new object is constructed each time (if, say, the class is meant to be immutable). Certainly when this is used as a builder pattern I would hope the same object is returned, though :)
@dlev ofcourse this is just basic example, the main use case is in immutable objects
1

Each of those methods return an instance. For example, the call to

obj.addX(3)

will return the same instance obj, so the call

obj.addX(3).addY(4)

will be equivalent to

obj.addY(4)

This is called method chaining.

The methods are implemented like this:

public SomeClass addX(int i) {
    // ...
    return this; // returns the same instance
}

Comments

0
public class Test1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Test1 abc = new Test1();
        abc.add1(10, 20).sub1(40, 30).mul1(23, 12).div1(12, 4);
    }
    
    public Test1 add1(int a, int b)
    {
        int c = a + b;
        System.out.println("Freaking Addition output : "+c);
        return this;
    }
    
    public Test1 sub1(int a, int b)
    {
        int c = a - b;
        System.out.println("Freaking subtraction  output : "+c);
        return this;
    }
    
    public Test1 mul1(int a, int b)
    {
        int c = a * b;
        System.out.println("Freaking multiplication  output : "+c);
        return this;
    }

    public Test1 div1(int a, int b)
    {
        int c = a / b;
        System.out.println("Freaking divison  output : "+c);
        return this;
    }
}

1 Comment

This code does not appear to answer the questions that were asked. I you beleive it does, please edit your answer to address the specific questions that the OP has raised.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.