Java Method Chaining
Jakob Jenkov |
Method chaining is a technique that enables you to express more functionality with less code. Sometimes method chaining is referred to as "fluid style". In this tutorial I will explain how method chaining works.
If you prefer video, I have a video version of this method chaining tutorial, here:
To see how method chaining affects your code, let us first look at a code example that does not use method chaining:
public class Node {
public List<Node< children = new ArrayList<Node>();
public void addChild(Node child) {
children.add(child);
}
}
Node root = new Node(); Node child1 = new Node(); Node child2 = new Node(); Node child3 = new Node(); root.addChild(child1); root.addChild(child2); root.addChild(child3);
In the code samples above, the Node class is not designed to support method chaining. As a result, we have to
refer to the root variable explicitly every time we call addChild() .
If we change the design of the Node class a bit, so the addChild() method returns a reference to the
Node instance it is called on - the code would look like this:
public class Node {
public List<Node< children = new ArrayList<Node>();
public Node addChild(Node child) {
children.add(child);
return this;
}
}
Noice how the addChild() method returns a reference to the Node instance it is called on - by
returning this. Now we can write the code in the previous example like this:
Node root = new Node();
Node child1 = new Node();
Node child2 = new Node();
Node child3 = new Node();
root.addChild(child1)
.addChild(child2)
.addChild(child3);
Notice in the last 3 lines of the example above that it only references the root variable once.
The 2 following calls to addChild() are called on the Node instance returned by the previous
addChild() call.
You can use method chaining at multiple levels in your code. Here is an example of using method chaining at multiple levels:
Node root = new Node().addChild(
new Node().addChild(
new Node().addChild(
new Node()
)
)
).addChild(
new Node().addChild(
new Node().addChild(
new Node()
)
)
)
Method Chaining Limitations
Method chaining has some limitations. These
- No up-reach
- No self-reach outside call chain
By up-reach, I mean that a child node in the example above cannot reach "up" to its parent Node - because the parent Node instance is not assigned to a variable by the time the child Node is created.
By self-reach, I mean that the child can only reach itself while directly on the call-chain. So, this code is valid:
new Node()
.addChild( new Node() )
.addChild( new Node() )
.addChild( new Node() )
;
But - this could not be possible:
new Node().setName( self.getName().toUpperCase() );
Inside the setName() method call, it is not possible to reach the Node instance the
setName() method is called on.
Scoped Access As Alternative to Method Chaining
For a fix to the up-reach and self-reach limitations, you can use scoped access. I have explained scoped access in the article about scoped access.
| Tweet | |
Jakob Jenkov | |











