p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning Android Application Development (http://p2p.wrox.com/forumdisplay.php?f=631)
-   -   Ch1 P.37 Code Mystery (http://p2p.wrox.com/showthread.php?t=84564)

John Moore August 2nd, 2011 06:09 PM

Ch1 P.37 Code Mystery
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setTitle("This is a dialog with some simple text...");

The above code fragment has a syntax I do not recognize.
First, I always thought that a return statement immediately returns from the method.

I get the impression that the new AlertDialog.Builder Constructor returns an object that is then referenced. However, I see the .setIcon method and the .setTitle methods placed in a manner similar to the visual basic with statement.

Can someone explain what's going on?

hhzz August 28th, 2011 01:49 PM

you are correct in your understanding

You are correct, the return will return right away. What's going on here is that each


statement is setting various properties of the object AlertDialog, as the book mentioned:
"You set the various properties, such as icon, title, and buttons, as well as checkboxes:"
Hope this makes sense.

John Moore September 2nd, 2011 10:26 AM

Possible Explanation of How the P.37 Code Works!
I also found out after researching the AlertDialog.Builder class that the methods of the class return a reference to the same instance of the AlertDialog.Builder class. So in effect, you've got a chain of method calls, each using the returned reference.

I wrote the following test to demonstrate how this works:

public class C1 {
        public C1(){
                System.out.println("C1 Constructor Called");
        public C1 M1(){
                System.out.println("M1 Method Called");
        public C1 M2(){
                System.out.println("M2 Method Called");
        public C1 M3(){
                System.out.println("M3 Method Called");
        public C1 create(){

public class test {

        * @param args
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                C1 cONE = doTest();
                System.out.println("Call method M3 via returned reference!");
        static public C1 doTest(){
                int choice = 0;
                case 0:
                        return new C1()
                return null;

Each method of the C1 class returns this - which is the key to being able to chain the methods in the switch statement above. The conventional way to write the switch statement would have been as follows:

  case 0:
        C1 cOne = new C1();
        return cOne;

Whether the new way is more efficient than the conventional way is the question? Anyone have an opinion?

rotter September 2nd, 2011 04:11 PM

So the function of return is to a) return control to the caller b) start a new thread of execution in which the remaining code is executed?

Alternatively, return is setting the value to be returned when the time comes, which is when the code in the procedure is exhausted?

Just a new boy making wild guesses.

All times are GMT -4. The time now is 04:46 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.