×

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.


Related Topics

JDBC Architecture

JDBC: JDBC stands for Java Database Connectivity. Sun Microsystems has a specification called JDBC. JDBC is a Java API (Application Programming Interface) that enables users to interact or communicate with...

4 minutes read.

Skyline Problem in Java

The skyline of a city is the outer edge of the pattern created by all of its structures when viewed from a distance. Return the skyline that these buildings together...

4 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

What is anagram in Java?

In this section will explain what an anagram is in Java and demonstrate how to determine whether or not a text is an anagram. In Java interviews, the anagram Java...

4 minutes read.

Dictionary in Java

Dictionary in Java The Dictionary class represents a key-value relation which maps keys to values. In Dictionary class, every key and every value is an object. It is an abstract class associated with...

2 minutes read.

public static void main string args meaning in java

In java main() method is the initial point for execution of the program. If a program doesn’t contain the main method, the program will not execute. JVM(java virtual machine) starts...

3 minutes read.

Short Circuit Logical Operators in Java

When there are two or more relational expressions in a decision-making statement, logical operators are utilized to combine them. The logical operators short circuit and not-short circuit fall into two...

5 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

Add numbers represented by Linked Lists in Java

For calculating the sum of the two numbers that are represented by a linked list, and then store the result in a new linked list. A linked list's head node...

7 minutes read.

How to take String Input in Java

There are various ways to take String input in Java. In this section, we are going to discuss how to take String input in Java. There are following ways to...

5 minutes read.

Java String length() method

Java String length() method returns length of the characters in a String. Syntax: public int length() Returns It returns length of the characters Java String length() method example 1          public class JavaStringLengthEx1  ...

1 minute read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Java While Loop

A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution...

1 minute read.

Java Try Resource

In Java, a try argument that specifies one or more resources is known as a try-with-resources statement. When your program is finished utilizing an object, it must be closed, which...

4 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

How to Convert char to String in Java

How to Convert char to String in Java There are two methods to convert char to String: Using String.valueOf(char) method Using Charcter.toString(char) method Using String.valueOf(char) method valueOf(char) is the static method of String class that...

2 minutes read.

Java 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 minutes read.

Quick Sort in Java

Quick Sort in Java Like merge sort, quick sort also uses the divide and conquer approach to sort the given array or list. In quick sort, the sorting of an array...

6 minutes read.

Java Volatile Keyword

The compiler, runtime, or processors may use any kind of optimization if there aren't any required synchronizations. Although most of the time these improvements are advantageous, they occasionally can result...

6 minutes read.

How to create a mirror image of a 2D array in Java

Problem Statement We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an...

2 minutes read.