| Author |
Method Chaining question
|
Saeed Amer
Ranch Hand
Joined: Jan 20, 2004
Posts: 135
|
|
Hi there
I am almost 100% sure that method-chanining is supported in Java. I said "almost" because following chaining doesn't compile.
Here is the original piece of code:
I wanted to do something like this:
but this code doesn't compile.
OR in other words, why can't I do the following:
Could someone please elaborate the concept why Java compiler is not allowing to do this?
Regards,
Saeed
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Could someone please elaborate the concept why Java compiler is not allowing to do this?
When you say this...
The compiler reads it as this...
Basically, you are asking the compiler to create a new GregorianCalendar object; using that object, call the set() method; and using whatever is returned from the set() method, call the getTimeInMillis() method. Since the set() method does actually return anything -- you can't dereference it to get to the getTimeInMillis() method.
If the set() method returns a copy of the original object -- like the methods of the StringBuffer class -- you can chain the methods.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Saeed Amer
Ranch Hand
Joined: Jan 20, 2004
Posts: 135
|
|
Great - thank you for so simple explanation.
Regards,
Saeed
|
 |
 |
|
|
subject: Method Chaining question
|
|
|