Introduction to method chaining in Java
Objective: This segment introduces how to use method chaining in Java with examples.
Method chaining is a technique in object-oriented programming to invoke multiple methods using a single object. Here, each method returns an object, and we can call methods in a single statement using the dot(.) operator. The advantage of method chaining is that it improves the readability and less code required.
Implementation of Java method chaining
KW.java
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW 25622348989 | James Moore | Phoenix 25622348992 | Ryan Bakshi | Mumbai kodingwindow@kw:~$
Implementation without method chaining
KW.java
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW 25622348989 | James Moore | Phoenix 25622348992 | Ryan Bakshi | Mumbai kodingwindow@kw:~$
What Next?
Java program to print the current date and time
Java implementation of Calendar class
Java Methods
Advertisement