Method Chaining is the practice of calling different methods in a single line instead of calling different methods with the same object reference separately. Under this procedure, we have to write the object reference once and then call the methods by separating them with a (dot.). It must be your foremost question, how will it work and how to use it?.
Let’s audit the example first and then it will be much smoother to explain.
Example 1:
class A { private int a; private float b; A() { System.out.println("Calling The Constructor"); } int setint(int a) { this.a = a; return this.a; } float setfloat(float b) { this.b = b; return this.b; } void display() { System.out.println("Display=" + a + " " + b); } } // Driver code public class Example { public static void main(String[] args) { // This will return an error as // display() method needs an object but // setint(10) is returning an int value // instead of an object reference new A().setint(10).display(); } } |
Compilation Error in java code:-
prog.java:34: error: int cannot be dereferenced
new A().setint(10).display();
^
1 error
Let’s Figure Out The Above Example:
- When we are calling the constructor, one should perceive that constructor does not holds any return type BUT it returns the current object reference. Open this for more about constructors.
- As object reference is returned by the constructor, we can use the returned object reference for calling another method as well.
- Thus, by implementing dot(.) operator we can call another method too, named “setint(10)”. As of now, we are trying to call the display method further, but, it’s impossible. Why? check out the next point.
- Now, “setint(10)” method is returning integer value of the variable and in a common way, one can easily understand that next method can’t be called on the basis of a variable. To solve this, “setint(10)” method must return object reference. How it can be done?.
Example 2:-
class A { private int a; private float b; A() { System.out.println("Calling The Constructor"); } public A setint(int a) { this.a = a; return this; } public A setfloat(float b) { this.b = b; return this; } void display() { System.out.println("Display=" + a + " " + b); } } // Driver code public class Example { public static void main(String[] args) { // This is the "method chaining". new A().setint(10).setfloat(20).display(); } } |
Calling The Constructor Display=10 20.0
Output : Calling The Constructor
Display=10 20.0
- In the above example, we have derived setint(int a) & setfloat(float b) method as the class type.
- In this case, while returning we are using “this” and it is returning the current instance reference. Check this for the uses and values of “this” reference variable.
- When the method chaining is implemented in the main method, “setint(10)” & “setfloat(20)” are returning the object’s reference which is further used to call “display()” method.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Constructor Chaining In Java with Examples
- Implementing our Own Hash Table with Separate Chaining in Java
- Program to implement Separate Chaining in C++ STL without the use of pointers
- Difference between Backward and Forward chaining
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.BitSet class in Java with Examples | Set 1
- Java.util.Collections.frequency() in Java with Examples
- Java.util.Arrays.equals() in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : subham223

