lilott8
lilott8

Reputation: 1116

Java consecutive method calls

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?

Upvotes: 0

Views: 1363

Answers (4)

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;
    }
}

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245429

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.

Upvotes: 7

Jigar Joshi
Jigar Joshi

Reputation: 240908

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

Upvotes: 2

Christian Tapia
Christian Tapia

Reputation: 34146

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
}

Upvotes: 1

Related Questions