Updated March 13, 2023
Definition
The technique of method chaining in java enables the developer to write readable and concise code that is easy to modify and maintain. a way of invoking methods on objects with a single statement. This technique was first seen in the C programming language, where the concept made certain tasks easier. Using method chaining, developers can create methods of complex chain that is used to perform multiple operations on the object without any need for intermediate variables. While returning the object after each called method, method chaining allows more streamlined and efficient code, which can improve performance.
Introduction to Method Chaining in Java
Method chaining is a powerful and flexible programming technique allowing developers to write more readable and concise code. This technique involves calling multiple methods in the same object in a single line of code by returning the object after the call of every method. We can use this technique widely in java frameworks and libraries such as stream API to perform large operations on objects and collections. Method chaining is also used to create interfaces that provide a more expensive and natural way to interact with the objects. Fluent interfaces allow the developers to chain the methods in a read-like sentence.
Key Takeaways
- A method chain is a series of methods that have access to the same objects and can be executed in succession by the JVM.
- Method chaining has many advantages over using conventional flow control structures such as if-else, for-each loops, and switch statements.
- Method chaining is used to improve the maintainability and readability of a code. Also, it will provide an expressive way to interact with the objects.
What is Method Chaining?
Method chaining is a programming technique where the execution of one method is replaced with another method. This process continues until an exit condition is met and execution stops. A method call has no parameters. This can be used to simplify the code, especially when performing many commands in a loop (for example, executing commands both on the first and second iteration)
By using this procedure, we have to write object reference at one time, and then we need to call the methods while separating it with the dot. Method chaining in java is the common syntax used to invoke the calls of multiple methods in OOPs. Every method in chaining returns the object. It is used to violate the needs of intermediate variables. The method chaining in java is also defined as an object that calls the object one by one.
In the below example, we have defined three methods in a single line.
Syntax:
Object_name.method_1().method_2().method_3();
In the above syntax, we have the object name as “object_name”. Once we have defined the object, then we call the methods one by one. Below example shows to call the constructor using multiple methods as follows.
Code:
class meth_chain {
private int abc;
private float pqr;
meth_chain() { System.out.println("Constructor call"); }
public meth_chain setint(int abc)
{
this.abc = abc;
return this;
}
public meth_chain setfloat(float pqr)
{
this.pqr = pqr;
return this;
}
void display()
{
System.out.println("Display = " + abc + " " + pqr);
}
}
public class Exp {
public static void main(String args[])
{
new meth_chain().setint(20).setfloat(30).display();
}
}
Output:
Explanation of Method Chaining in Java
Method chaining is the method that is called one method after another method. In Java, it is the same as constructor chaining, but the only difference is the constructor and method. The method chaining term refers to the convention and design. Every method in chaining will return the object, allowing user to call the chained together in a single statement. The chaining method is applied to methods and classes as follows.
- Multiple methods are potentially going and are called in the same object.
- The methods in the object return the value.
In Java, we are using method chaining techniques extensively by using classes. Method chaining is also used to provide fluent API. Fluent API is used to design sentences of natural language. It is composed of multiple methods that are chained together to form an expression. The below example shows how we can define the method chaining in java as follows.
Code:
class meth_chain {
private int abc;
private float pqr;
meth_chain() { System.out.println("Constructor call"); }
public meth_chain setint(int abc)
{
this.abc = abc;
return this;
}
public meth_chain setfloat(float pqr)
{
this.pqr = pqr;
return this;
}
void display()
{
System.out.println("Display = " + abc + " " + pqr);
}
}
public class Exp {
public static void main(String args[])
{
new meth_chain().setint(20).setfloat(30).display();
}
}
Output:
Examples of Method Chaining in Java
Below example is shown without method chaining. We have not defined any chaining method in the given code.
Code:
public class Stud {
private String sname;
private int sclass;
private int num;
public void setSname (String sname) {
this.sname = sname;
}
public void setSclass(int sclass) {
this.sclass = sclass;
}
public void setNum(int num) {
this.num = num;
}
public void detail() {
System.out.println("Stud" + sname + ". It's class is " + sclass + ". Stud num "+num);
}
public static void main(String args[]) {
Stud stud = new Stud();
stud.setSname("ABC");
stud.setSclass(2);
stud.setNum(40);
stud.detail();
}
}
Output:
In the below example, we are invoking the method by using method chaining as follows.
Code:
public class stud {
private int stud_id;
private String stud_name;
private int stud_age;
private int std;
public stud setId(int stud_id) {
this.stud_id = stud_id;
return this;
}
public stud setstud_Name(String stud_name) {
this.stud_name = stud_name;
return this;
}
public stud setstudAge(int stud_age) {
this.stud_age = stud_age;
return this;
}
public stud setStd(int std) {
this.std = std;
return this;
}
public void detail() {
System.out.println("stud Detail is:\n");
System.out.println("stud_id: "+stud_id+ "\nstud_name: "+stud_name+"\nstud_age: "+stud_age+ "\nStandard: "+std);
}
public static void main(String args[]) {
stud Stud = new stud();
Stud.setId(11).setstud_Name("ABC").setstudAge(10).setStd(5).detail();
}
}
Output:
Conclusion
Method chaining techniques are used to improve the program’s efficiency by reducing the temporary variables. By using method chaining techniques, we can simplify the code while reducing the number of lines that are needed to perform the task. Method chaining techniques allow us to do the more complex operations performed on data.
FAQs
Q1. How does method chaining work in java?
Every method in the chain will return the object we have called as per the returned object. It allows multiple method calls on the same object in single-line code.
Q2. What are the benefits of method chaining?
Method chaining is used to make our code more readable, make the code more reusable and modular, and reduce the need for temporary variables.
Q3. What is an example of method chaining in java?
Method chaining is used in frameworks and libraries such as spring framework, java streams, and Hibernate ORM.
Recommended Article
We hope that this EDUCBA information on “Method Chaining in Java” was beneficial to you. You can view EDUCBA’s recommended articles for more information.