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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With