Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help understanding this syntax -- new object returned

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help understanding this syntax -- new object returned

    I was reading an Android tutorial and came across this block of code:

    protected Dialog onCreateDialog( int id )
    {
    	return new AlertDialog.Builder( this )
            	       .setTitle( "Planets" )
            	       .setMultiChoiceItems( _options, _selections, new DialogSelectionClickHandler() )
            	       .setPositiveButton( "OK", new DialogButtonClickHandler() )
            	       .create();
    }



    Can someone explain how this works? How can you invoke the setTitle(), setMultiChoiceItems(), and other methods like this without specifying an object name. I'm sure it's correct -- I've just never seen it before.

    Normally I would do something like:
    protected Dialog onCreateDialog( int id )
    {
     
        Dialog d = new AlertDialog.Builder(this);
        d.setTitle( "Planets" );
        d.setMultiChoiceItems( _options, _selections, new DialogSelectionClickHandler() );
        d.setPositiveButton( "OK", new DialogButtonClickHandler() )
        d.create();
     
        return d;
    }



    Does anyone know what that type of initialization used in the tutorial is called so I can read about it? I tried to look it up in a java reference book but couldn't find anything on it.

    Thanks for your help.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,107
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Help understanding this syntax -- new object returned

    The compiler is ignoring white spaces.
    Edit out the white spaces to get:
    AlertDialog.Builder( this ).setTitle( "Planets" ).setMultiChoiceItems( _options, _selections, new DialogSelectionClickHandler() ).setPositiveButton( "OK", new DialogButtonClickHandler() ).create();

    Each of the methods must return an object that can be used by the next method. <<<<<<<< Check this in the doc

    Really ugly code. Must be generated by an IDE.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help understanding this syntax -- new object returned

    You are absolutely correct. I cannot believe I didn't realize it myself. I've been away from java for a while so I assumed this was some new syntactic sugar. I agree with you that it's quite ugly, but it's not auto-generated code, it's from a book on developing android applications. Maybe this is a java convention when chaining methods?

    Thanks for your help. I have a hard time moving on with stuff when I don't completely understand what's happening.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,107
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Help understanding this syntax -- new object returned

    Maybe with the small windows they want everything in a narrow column and with as few statements as possible. Although I think the compiler has to generate variables to hold the returned objects so I'm not sure the generated code would be any smaller.

  5. #5
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Help understanding this syntax -- new object returned

    The compiler code, as I understand it, would just do...
    create AlertDialog.Builder
    store
    call setTitle
    store
    call setMultiChoiceItems
    store
    call setPositiveButton
    store
    call create
    store
    return
    On the top one anyway

    The second one would be optimized down to doing the same thing, and it would be a lot prettier

Similar Threads

  1. Help me Understanding Please...
    By Jabetha in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 17th, 2011, 01:55 PM
  2. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  3. Ok, Notepad class has returned!
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 1st, 2011, 09:50 PM
  4. Error returned
    By elie winter in forum JDBC & Databases
    Replies: 1
    Last Post: October 17th, 2010, 11:49 PM
  5. how to reuse method returned string ??
    By zeeshanmirza in forum Collections and Generics
    Replies: 7
    Last Post: September 4th, 2009, 03:54 PM