×

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

Zig Zag star and Number Pattern in Java

We covered many Java pattern applications in the preceding part. We will write Java applications for zigzag star and number patterns in this part. Printing Zig Zag Number Pattern Steps Print one...

3 minutes read.

Aggregation in Java

Aggregation A "has-a" and "whole/part" connection is the best way to define the aggregate relationship in Java, which exists between two classes. This connection relationship has a higher level of specialisation....

4 minutes read.

Java Virtual Machine (JVM)

JVM is a virtual runtime environment to execute Java byte codes. The JVM doesn’t understand the keywords we used to write code. That is why it is converted into bytecode. It controls the...

3 minutes read.

Java String concat() method:

Java String concat() method is used to add the given String to the end of the current String. Syntax: public String concat(String str) Parameter: Str: String to be concatenated at the end of current...

1 minute read.

Web Service Response Time Calculation in Java

Administration response time or reaction time is the typical measure of time it takes for a solicitation to be handled by a PC framework, like your organization switch. In the...

4 minutes read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

Java Integer min() method

The min()  method of Integer class returns the smaller of two int values. It returns the same result as by calling Math.min.  Syntax public static int min(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

How to run Java Program?

How to run Java Program In this section, we will learn how to write, compile and run a Java program in Command Promptusing notepad. In order to run a Java program, we...

2 minutes read.

How to Create Immutable Classes in Java

Introduction Java is a programming language that is entirely object-oriented, and everything in it is seen as an object. And the blueprint or template of these objects are classes. Several classes...

3 minutes read.

India Map Pattern in Java

India Map Pattern is the pattern we'll code now using Java, as demonstrated. We will use a star in Java to print our India map.The specialty is that we will only...

4 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

Generic queue in Java

Before understanding how to implement a generic queue in java, one must know about generics and queue in java. Generics in Java Generics are parameterized types. The goal is to enable type...

6 minutes read.

Stone Game in Java

In this tutorial, we will learn to design a stone game in Java. First of all, we will understand what is this game all about. We will grasp it through...

9 minutes read.

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

IdentityHashMap in Java

The IdentityHashMap class is comparable to the HashMap class and is an AbstractMap implementation. However, when comparing the key, it uses reference equality rather than object equality (or values). Identity HashMap...

6 minutes read.

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

How to Convert char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

How to Convert String to Date in Java

How to Convert String to Date in java You can convert String to Date in Java by using the parse() method. There are two classes which have parse() method namely, DateFormat and SimpleDateFormat classes....

2 minutes read.

How to Calculate Week Number From Current Date in Java?

The WeekFields class's weekOfMonth() method is utilized to return the field for access the week of a month based on this WeekFields. If the first day of the month is a...

3 minutes read.

Magnanimous Number in java

Magnanimous Number When the left and right halves of a majestic number are combined, the result is invariably a prime number, which must have at least two digits. The number's left...

3 minutes read.