Method chaining in Java
Method chaining in Java is the act of calling a series of methods one after the other. The sole distinction between it and function Object () constructor chaining is the difference between the method and function Object () constructor. We will talk about Java's method chaining in this part.
A popular syntax for calling many methods simultaneously in OOPs is method chaining in Java. Chaining methods each return an object. The requirement for intermediate variables is broken. In other words, method chaining may be described as what happens when we have an object and call the methods on it.
What is Method chaining?
In OOPs, method chaining is a popular syntax for calling numerous methods at once. Chaining methods each return an object. The requirement for intermediate variables is broken.
In other words, method chaining may be described as what happens when we have an instance and call the methods on it one after another. For instance,
object.methodA().methodB();
The sentence above uses an object (Object) and calls methods A and B. Method chaining is the process of calling or invoking multiple methods at once.
It is sometimes referred to as a named or parameter idiom. Although there are frequently added line breaks between ways, it is sometimes also referred to as a train wreck due to the rise in their number.
Applications of Method chaining
- Method chaining is used to put technique cascading into practice.
- Method chaining is also utilized in fluent interface implementation.
Note: Java method chaining is often referred to as named parameter idiom or parameter idiom. Even though there are frequently added line breaks between ways, it is sometimes also referred to as a train wreck due to the rise in their number.
Calling methods without method chaining in Java
The method-changing idea is not used in the following Java program.
Method.java
import java.io.*;
public class Method
{
private String name;
private int c;
private int no;
public void name (String name)
{
this.name = name;
}
public void cost (int c)
{
this.c = c;
}
public void Qua (int no)
{
this.no = no;
}
public void show ()
{
System. out.println("Name of the product here " + name + ". \nCost of the product " + c + ". \nQuantity of the products available "+ no);
}
public static void main(String args[])
{
// Object creation for the above product class
Method product = new Method();
// Invoking the product class methods using the method name
product.name("Tv");
product.cost (30499);
product.Qua(1);
product.show ();
}
}
Output
Name of the product here Tv.
Cost of the product 30499.
Quantity of the products available 1
In the above code, we have created a class showing the product details where we use the general method for the method class that uses the Object designed for the particular class.
Then we initialized the product details using the constructor and called the methods separately using the Object created for the class.
In this program, we are not using the methods of method chaining. We are calling the methods by creating the Object in class and calling the methods through the Object created.
Calling methods with method chaining in Java
Method1.java
public class Method1
{
private int Sid;
private String Sname;
private int Sage;
private int Sclas;
public Method1 setSid(int Sid)
{
this.Sid = Sid;
// Used to the chaining of the methods, so we use this reference
return this;
}
public Method1 Sname(String Sname)
{
this.Sname = Sname;
return this;
}
public Method1 Sage(int Sage)
{
this.Sage = Sage;
return this;
}
public Method1 Sclas(int Sclas)
{
this.Sclas = Sclas;
return this;
}
public void Show()
{
System.out.println("Details of the student are :\n");
System.out.println("Student id: " + Sid + "\nStudent name: " + Sname + "\nStudent age: " + Sage + "\nStandard: " + Sclas);
}
public static void main(String args[])
{
Method1 student = new Method1 ();
// Using method changing method
student.setSid(1183).Sname("Manoj Mamilla").Sage(19).Scales(12).Show();
}
}
Output
Student id: 1183
Student name: Manoj Mamilla
Student age: 19
Standard: 12
In the above code, we have created a class showing the student details where we use the method chaining technique for the method1 class that uses the Object created for the particular class.
Then we initialized the student's details using the constructor and called the methods using the method chaining technique using the Object created for the class.
In this program, we are using the method of chaining. We call the methods using the method of chaining by creating the Object to class.