• 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
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

What "return this" means in Java

 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

In crawljax open-source code, I seen this method,



I got, it should return "Options". But, "return this" ? I dint get the meaning of this. Because in the above code, they called method addOption(Params...); they are not catching anything from that method then how can they return "return this" ? Please explain me what "return this" means.

Thanks All:
Ramakrishna K.C
 
Ranch Hand
Posts: 119
Spring MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"return this" means return the current object instance. In your example it will return the Options object as shown by the addOption() method. For more information on the this keyword you can read the Java language specification.
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply,

It'll return the instance of . This is nothing but,



I'm I correct?
 
I Wayan Saryada
Ranch Hand
Posts: 119
Spring MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The addOption() method from your first example must be part of a class right? For example this class might be look like as simple as this:



So, the return this simply means return me the current object instance, which is the instance of the class Options. The keyword this is a reference to the current object.
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The addOption() method from your first example must be part of a class right?

Obviously yes. But, they called addOption() method and catching nothing. I mean,



see the above code. The above code from another class. Creating instance of Options and then calling options.addOption(Params....); they called and that method is returning something like "return this". But, they are not capturing "this" instance. Then, why they are returning "this"?
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Returning this allows several calls on the same object to be "chained". Most often I see this used with a StringBuilder:
This is functionally identical to the following code:but requires less typing and, at least for some people, is more readable (this is obviously subjective).

Note that the append() methods in the second example returns this as well, but the caller just doesn't use it. You aren't required to "use" the value returned by a method in Java, but returning "this" allows to chain the methods as outlined in the first example.

Returning this is also used by so called Fluent interfaces. However, the concept of fluent interfaces is broader than just returning this - fluent interfaces always return the instance that is best suited for the particular context of the operation that was performed. (Uh, I hope the Wikipedia article makes better job of explaining it than I did here.)
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramakrishna Udupa wrote:

The addOption() method from your first example must be part of a class right?

Obviously yes. But, they called addOption() method and catching nothing. I mean,



see the above code. The above code from another class. Creating instance of Options and then calling options.addOption(Params....); they called and that method is returning something like "return this". But, they are not capturing "this" instance. Then, why they are returning "this"?



Hi,

As per your statement addOption() method "return this". It means this method is returning the instance of the object by which this method is called so that you can call anathor methods on the same object in a chain like :

options.addOption(...).addOption(...).addOption(...) and so on

I hope it clears now.

Regards,
Saurabh Agrawal
 
There's a way to do it better - find it. -Edison. A better tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic