aspose file tools*
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 Java 8 in Action this week in the Java 8 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: 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: 18136
    
  39

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