• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

syntax of a method call

 
Jarrod Legion
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic