This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Method Chaining question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method Chaining question" Watch "Method Chaining question" New topic
Author

Method Chaining question

Saeed Amer
Ranch Hand

Joined: Jan 20, 2004
Posts: 140
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: 18545
    
  40

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: 140
Great - thank you for so simple explanation.

Regards,
Saeed
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Chaining question
 
Similar Threads
java date beginner question
Comparing dates...
time comparison between given periods
Calendar Class
How to check for two dates equality in HQL ?