Invocation Chaining


Invocation chaining means that you are not limited to merely accessing one class/object member in a given statement with the . operator but may continue to access further members in a given statement. For example, let's say that we wanted to convert an integer value to a String object representation and then retrieve the first digit from the string as a character. We might perform this task as follows:

int i = 72;
   String str = String.valueOf(i);
   char firstChar = str.charAt(0);
   System.out.println(firstChar);   // prints 7

This code is perfectly fine, but we could have also implemented this code in a neater fashion using invocation chaining as follows.

int i = 72;
   char firstChar = String.valueOf(i).charAt(0);
   System.out.println(firstChar);    // prints 7 also

It's quite easy to see how this works. The . operator has a left (left to right) precedence.. With this in mind, we can see that the following statement is evaluated first of all:

String.valueOf(i)

This will return a new String object representation of the integer variable i passed to it. Then the method charAt is invoked on the new String object, returning the first character in the string to the variable firstChar. You should look at the statement String.valueOf(i) as a reference to the String object itself, which it is, as this is what the method returns. You can then access members of the String object like charAt that we accessed.

If we said that we had a Person object inside a Planet object that in turn was inside a SolarSystem object, and the SolarSystem object was inside a Universe object, we may access the Person object from a reference to the Universe object as follows.

Person bob = myUniverse.mySolarSystem.myPlanet.myPerson;

Legal Disclaimer

Our website is not responsible for the information contained by this article. Webworldarticles.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.


This article was sent to us by: Gabriela C. Perez at 01032008

Related Articles

1. Importing Java Packages
To use a package within a Java application or applet, we need to import it. We do this by means of the import keyword. So, for example, if we wish to include the I/O packag...

2. What is a Java Package
A Java package is a collection of related classes that can be imported into your program to support your software. They also provide namespace management, as wel...

3. Character Escape Sequences
Character escape sequences allow for a character to be interpreted differently than its literal value. Character escape sequences are defined using the backslash (\) ch...

4. Conditional Statements
The ability to choose the path that your program takes, based on any given data, is the key to all functionality in programming. In order to create conditional statemen...

5. Java Methods
Methods are used as the building blocks of your program, performing tasks that can be called again and again and using the same code to perform the task each time. The basi...

6. Variable Scope
The scope of a variable is the area in which a variable belongs, specified by the area in which it is declared. The following example code contains two declared variables, ...

7. Regular Expressions in Java
A regular expression is a code that is used to match a pattern in a given string and is new to Java 1.4. Regular expressions are made up of normal characters and metacharac...

8. Introduction to Object Oriented Programming (OOP)
The transition from a procedural programming (non-OOP) language to an object-oriented programming language is a large step for many programmers. It is true that both method...

9. Operator Precedence in programming languages
Operator precedence deciphers the order in which calculations in an expression occur. Looking at the calculation example 3 + 4 * 6, the answer could be calculated by ad...

10. Arithmetic Assignment Operators
The following assignment operators are similar to the increment and decrement operators that we have just seen. They are used so that you do not need to enter the sourc...