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 3 of 3

Thread: Question about inline code segment

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

    Default Question about inline code segment

    Not sure if this is in the appropriate topic, if not let me know where would be better, thankyou.

    Fairly new to object oriented programming and even newer to Java. I've never seen code inlined like this before. I need help understanding what exactly is happening here. Is this segment of code overloading the constructor of the OnClickListener object?

     
     
     
    OnClickListener l = new OnClickListener()
    {
    	public void onClick(View v) 
    	{
    	             if (mPhoneIsSilent)
    		{
    			mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    			mPhoneIsSilent = false;
    		}
     
    		else
    		{
    			mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
    			mPhoneIsSilent = true;
    		}
     
    		toggleUi();
    	}    	    
    };


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Question about inline code segment

    Is this segment of code overloading the constructor of the OnClickListener object?
    No, it's extending OnClickListener if OnClickListener is a class, implementing OnClickListener if OnClickListener is an interface. It's an anonymous inner class:
    Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    What is overridden (or implemented) is the onClick(View) method.

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

    Default Re: Question about inline code segment

    Thank you. I looked all over the net for documentation about that but I didn't know what it was refered to as. (anonymous class)

Similar Threads

  1. Replies: 5
    Last Post: July 7th, 2011, 10:22 AM
  2. This is my code and i would like to ask a question..
    By savvas in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 24th, 2011, 02:01 PM
  3. Replies: 1
    Last Post: April 7th, 2011, 08:01 AM
  4. Swin/Awt Code Question
    By Pulse_Irl in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2010, 12:26 PM
  5. Compute the frequency count and big-oh notation for a certain code segment
    By maykel_trinidad in forum Java Theory & Questions
    Replies: 3
    Last Post: November 13th, 2009, 10:23 AM