• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Method Chaining question

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Saeed Amer
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great - thank you for so simple explanation.

Regards,
Saeed
 
Always look on the bright side of life. At least this ad is really tiny:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic