• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"concatenated" methods?

 
adam cannon
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do methods such as:

x = foo().bar();

Do the same thing as:

x = foo(bar());

I'm thinking no. I understand what the second statement does but the first?
 
Henry Wong
author
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The methods such as:

x = foo().bar();

Do the same thing as:

temp = foo();
x = temp.bar();

Basically, you are calling the bar() method, on the object that is returned by the foo() method.

Henry
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first bit of code calls the foo() method of the current class which returns an instance of a class that has a bar() method. The bar() method is then called on this returned object.
i.e. it is the equivalent of
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by adam cannon:
... x = foo().bar(); ...

Note: Be careful of this type of chaining if it's possible for a null reference to be returned. That is, if foo() returns null for some reason, then an exception will be thrown when you try to use it to call bar().
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

Note: Be careful of this type of chaining if it's possible for a null reference to be returned. That is, if foo() returns null for some reason, then an exception will be thrown when you try to use it to call bar().


heck, i avoid this kind of chaining all together. they can be incredibly difficult to debug...
 
camilo lopes
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because what is entries () have great preference!
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by camilo lopes:
because what is entries () have great preference!
I was taught that whatever is in the () is evaluated before anything outside the brackets; as far as I know that applies to () after the name of a method as well. What marc weber means is that it is easy to produce errors with many calls in a long chain. It is also difficult to work out what to do; how do you know which boo or foo or far or ooof to use?
 
adam cannon
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about:

x = foo.var.bar()

Does that mean whatever string is in var is the object reference for bar()?

Say if var = "randomObject", the above statement be essentially randomObject.bar()?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by adam cannon:
How about:

x = foo.var.bar()

Does that mean whatever string is in var is the object reference for bar()?

Say if var = "randomObject", the above statement be essentially randomObject.bar()?


No. That would call the bar() method on whatever object var currently referenced assuming the object had such a method. If var was a String, then you would get a compilation error because Strings don't have a bar() method.

In order for that code to work your foo class would have to have a member variable called var of type randomObject (assuming the randomObject class had a bar() method)
i.e.and assuming that var was initialised in the Foo constructor as I have shown above you could then type
[ April 04, 2008: Message edited by: Joanne Neal ]
 
adam cannon
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. That would call the bar() method on whatever object var currently referenced assuming the object had such a method.


Thanks but I'm a bit confused now. If var is also an object, then what is it doing being concatenated to foo?
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by adam cannon:
If var is also an object, then what is it doing being concatenated to foo?
It is precisely because you can get confused that it is a good idea to avoid this sort of notation. Programming, please, not join-the-dots!

If you write foo.var then you are referring to the var field of whatever foo is. You can tell it is an object not a method because there aren't any () after var.

If you had a method called var you would write foo.var()
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by adam cannon:

Thanks but I'm a bit confused now. If var is also an object, then what is it doing being concatenated to foo?


var is not an object, it is an instance variable that references an object.



If you have that class, the code



would print "11", the length of the String "some string".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic