"builder" is a local variable in scope of the onCreate method and is
used in the anonymous inner class (new OnClickListener).  Java keeps a
copy of that local variable inside the listener object so Final
ensures that the copy is consistent - you are not allowed to assign
builder to some other value later in the method.

If "builder" were a instance field it would not have to be Final since
inner classes can access fields in the containing object.

On Jan 20, 12:20 pm, TreKing <[email protected]> wrote:
> On Wed, Jan 20, 2010 at 11:35 AM, tim hansen <
>
> [email protected]> wrote:
> > final Builder builder = new AlertDialog.Builder(this);
>
> >          btnSave = (Button)findViewById(R.id.save_button);
>
> >          btnSave.setOnClickListener(new OnClickListener() {
> >           @Override
> >           public void onClick(View v) {
> >            builder.show();
> >           }
> >          });
>
> I am no expert in Java, but I believe in this case you need the final
> keyword to ensure that the "builder" variable that you're using in the
> anonymous OnClickListener is still valid when it's onClick() function is
> actually called.
>
> If it were not final, the variable "builder" would be invalidated as soon as
> you exited the onCreate() function. Later, when you actually press the save
> button and the onClick() function gets called, builder would be null (or
> complete garbage).
>
> I'm sure someone will correct me if I'm mistaken.
>
> -------------------------------------------------------------------------------------------------
> TreKing - Chicago transit tracking app for Android-powered 
> deviceshttp://sites.google.com/site/rezmobileapps/treking
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to