Question

Notification.Builder notificationBuilder = new Notification.Builder(
                        getApplicationContext())
                    .setTicker(tickerText)
                    .setSmallIcon(android.R.drawable.stat_sys_warning)
                    .setAutoCancel(true)
                    .setContentIntent(mContentIntent)
                    .setSound(soundURI)
                    .setVibrate(mVibratePattern)
                    .setContent(mContentView);

invokes multiple methods on the anonymous[--correction: it's NOT anonymous--] class. On each line it starts with .<method name>. Where is the official Java documentation on this? I'm hoping there is a page on http://docs.oracle.com/javase/ that explains it.

PS. I just learned this is called "method chaining". So where is the official Java documentation, if any, on this concept?

Was it helpful?

Solution

You ask:

So where is the official Java documentation, if any, on this concept?

If your method returns an object, you can call another method on the returned object, simple as that. All of the methods above return this, and so if method1, method2, and method3 all return this you can do this:

MyClass myVar = new MyClass().method1().method2().method3();

which is the same as:

MyClass myVar = new MyClass();
myVar.method1();
myVar.method2();
myVar.method3();

The documentation for Java method invocation can be found in the JLS Section 15.12.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top