This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello all, I am not able to understand the following method call, which is from Mughal's 1st edition on page 75: someObjRef.make().make().make()
Here is Mughal's explanation: make() method returns an object reference that indicates the object to execute the next call, and so on. Could someone please give an example code of the above reference and method so that I can see how they are structured, thus able to make sense of the above method syntax?
The lesson to learn in all of this is that the code that invokes a method that returns a value is also an expression, that expresses that value. So, a method that returns an int can be used in every which way that an int type variable can be used. I've not read the book, but I'd guess that it's attempting to exemplify this same idea, where a method that returns some object type reference can be used in the same manner that a reference to an object of that same data type can be used.
Making any sense, yet? [ December 05, 2003: Message edited by: Dirk Schreckmann ]
Let's rename SomeObjRef "REFERENCEtoObjectType1". ObjectType1.make() is a method that returns a type of object I'll call ObjectType2. ObjectType2.make() is a method that returns a type of object I'll call ObjectType3. You could write it REFERENCEtoObjectType1.make().make().make(); and you get a ObjectType3 in the end. Roughly the same thing happens here:
[ December 05, 2003: Message edited by: Elouise Kivineva ]
Joined: Dec 24, 2002
Thank you Dirk and Elouise. A follow up question: When using this type of method syntax-- foo.getNewFoo().getNewFoo().getNewFoo() (from Dirk's example) can it only be used when a method is returning some object and not for any other data type? TIA
Joined: Dec 10, 2001
That's correct. In Java, there are basically two categories of data types - objects and primitives. Primitives cannot have methods invoked on them - they're just primitive values. In Java 1.5, we'll likely have a seemingly different data type called an enum. These will actually just be special types of objects, and so they could have methods invoked on them as could "normal" objects.