aspose file tools*
The moose likes Beginning Java and the fly likes syntax of a method call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "syntax of a method call" Watch "syntax of a method call" New topic
Author

syntax of a method call

Jarrod Legion
Greenhorn

Joined: Dec 24, 2002
Posts: 18
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?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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 ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
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 ]
Jarrod Legion
Greenhorn

Joined: Dec 24, 2002
Posts: 18
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
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: syntax of a method call
 
Similar Threads
Overriding/overloading.
Upcasting of Classes Problem
Pass by Value
Anonymous inner class
On the Stack and What's Accessible