2
new AlertDialog.Builder(this)
  .setMessage(mymessage)
  .setTitle(title)
  .setCancelable(true)
  .setNeutralButton(android.R.string.cancel,
     new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton){}
     })
  .show();

Hi, could someone explain what this java language feature is called, to be able to call methods without directly specifying the object before the '.' ?

I would like to read more about how to use this...

and does it only work with "new" or can I use existing objects with this syntax?

ycomp
  • 8,316
  • 19
  • 57
  • 95
  • there is actually an object before each '.' if you put it on a single line you can see it clearer. new AlertDialog.Builder(this).setMessage(mymessage).setTitle(title).setCancelable(true) – aldrin Feb 22 '12 at 13:36
  • ok, I'm an idiot it is method chaining.. (well at least now I know this term, although I had used that functionality many times before)... I had just been confused because there are AlertDialog.Builder and AlertDialog objects and I looked at the docs of AlertDialog which had many of the same methods like .setMessage... except that in AlertDialog it returns void but in AlertDialog.Builder returns a Builder object. – ycomp Feb 22 '12 at 13:43

6 Answers6

6

This is called method chaining.

It does not require any special support from the Java language. Each of the functions in question (setMessage(), setTitle() etc) simply returns this, thereby allowing the chaining.

For more information, see How to do method chaining in Java? o.m1().m2().m3().m4()

does it only work with "new" or can I use existing objects with this syntax

Provided the methods return this, this technique can be used with or without new.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

It's called method chaining.

And you actually are calling the methods on an object:

new AlertDialog.Builder(this) returns an object.

Call setMessage(mymessage) on that object and you get another object, and so on.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • that is what I thought... but in the android docs it shows that these methods (At least the ones I looked at) return void... but when I paste the text into my java IDE, it passes syntax checking (no red squiggles) http://developer.android.com/reference/android/app/AlertDialog.html – ycomp Feb 22 '12 at 13:38
  • @ycomp the return type is void or it returns a null object? – Luchian Grigore Feb 22 '12 at 13:38
  • oh.. I think I'm looking at wrong object.. lol there are AlertDialog.Builder and AlertDialog objects. It is method chaining.. pity, I thought I'd discovered some equivalent of a "with" statement for java, lol – ycomp Feb 22 '12 at 13:39
1

Method Chaining. Each method returns a reference back to itself which allows you to call another method on it and so forth. jQuery uses this a lot in the JavaScript world.

akiller
  • 2,462
  • 22
  • 30
1

It is chainable syntax, it can be used with any object whose methods return this object.

sinsedrix
  • 4,336
  • 4
  • 29
  • 53
1

you are missing the point whitespace is ignored in most languages' code files therefore what you see is actually one long line split for readability

 new alertDialog.Builder(this).setMessage(mymessage).setTitle(title).setCancelable(true).setNeutralButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton){}}).show();

you get the point

Andrei G
  • 1,590
  • 8
  • 13
1

The line of code is terminated by the semicolon. What you have there is (formatting-aside) identical to:

new AlertDialog.Builder(this).setMessage(mymessage).setTitle(title).setCancelable(true).setNeutralButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton){}}).show();

I think you'll agree that the linebreaks make it more readable!

Now, each one of those methods in the chain is implemented as returning this as a useful "side effect" (in the intent sense, rather than functional sense) - this means that myBuilder.setMessage(myMessage) returns the modified myBuilder, which you're then free to use .setTitle(title) on, et cetera.

Kristian Glass
  • 37,325
  • 7
  • 45
  • 73