The 'final' keyword is not android specific.

It is a java keyword that declares that the variable-reference can
never be changed after it has been defined.

e.g.

final int x;
x = 10; // OK
x = 20; // Compiler error, since x is declared final and cannot be
changed.

final String age;
if (x = 0)
  age = "nothing"; // OK
else
  age = Integer.toString(x); //OK

if (somethingElse)
  age = someOtherValue; // Compiler error, since age is final and  may
already have been defined.

Why use final:
- Some compilers may be able to optimize better if a variable is
declared to be 'final'.
- Ensure that a variable doesn't get assigned another value
accidentally.

I'm not sure if this is true for the DalvikVM, but i tend to use
'final' a lot more for embedded device development. Maybe DalvikVM
will tend to use registers more for 'final' variables...?

For regular Java developent (J2EE), it is less important to use
'final'. It actually adds to the syntactic sugar.

On Jan 20, 12:35 pm, tim hansen <timhansen.lon...@googlemail.com>
wrote:
> Hi all,
> Can anyone explain why, when I use the Final keyword the following code
> works fine, but without it, it doesn't?
>
> I just find it irritating that I don't understand why it works! ;)
>
> Thanks in advance!
>
> Tim.
>
> public class PowerLangMaxMain extends Activity {
>
>      private Button btnSave;
>
>     �...@override
>      public void onCreate(Bundle savedInstanceState) {
>          super.onCreate(savedInstanceState);
>          setContentView(R.layout.main);
>
>           final Builder builder = new AlertDialog.Builder(this);
>
>          builder.setTitle("Confirm");
>          builder.setMessage("Save to Database");
>          builder.setPositiveButton("ok", null);
>          builder.setNegativeButton("cancel", null);
>
>          DBAdapter db = new DBAdapter(this);
>
>          btnSave = (Button)findViewById(R.id.save_button);
>
>          btnSave.setOnClickListener(new OnClickListener() {
>           @Override
>           public void onClick(View v) {
>            builder.show();
>           }
>          });
>      }
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to