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?
The line of code is terminated by the semicolon. What you have there is (formatting-aside) identical to:
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
thisas a useful “side effect” (in the intent sense, rather than functional sense) – this means thatmyBuilder.setMessage(myMessage)returns the modified myBuilder, which you’re then free to use.setTitle(title)on, et cetera.