File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes 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 ""concatenated" methods?" Watch ""concatenated" methods?" New topic
Author

"concatenated" methods?

adam cannon
Greenhorn

Joined: Apr 02, 2008
Posts: 6
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
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3162
    
  10
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


Joanne
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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().


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10908
    
  12

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...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
because what is entries () have great preference!


Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Apr 02, 2008
Posts: 6
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

Joined: Aug 05, 2005
Posts: 3162
    
  10
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

Joined: Apr 02, 2008
Posts: 6
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Jul 11, 2001
Posts: 14112
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".


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "concatenated" methods?
 
Similar Threads
return types scjp1.4
How to turn on assert in TextPad ?
Overridding!!
Multithreading on Linux
Question about Object.clone() discussion in the API