[android-developers] Re: Understanding how the Final keyword in Android Works
Hi. Im not sure if this topic is still active but could i ask you to elaborate? I checked a few youtube videos and the author says he declares final variables that will be saved if anyone leaves the app and comes back and he sets the value in onsaveinstancestate. The problem is he declares private final bill="Bill value". It seems to go against the meaning of final. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. To post to this group, send email to android-developers@googlegroups.com. Visit this group at https://groups.google.com/group/android-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/f8c4af6d-cdda-43b2-a3b3-51c9cf33f5fc%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[android-developers] Re: Understanding how the Final keyword in Android Works
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 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
[android-developers] Re: Understanding how the Final keyword in Android Works
"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 wrote: > On Wed, Jan 20, 2010 at 11:35 AM, tim hansen < > > timhansen.lon...@googlemail.com> 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 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
[android-developers] Re: Understanding how the Final keyword in Android Works
On Jan 20, 10:49 am, Burk Hufnagel wrote: > Tim, > > This is not an Android thing, it's a Java thing. Because the anonymous > instance of OnClickListener can exist after the onCreate method > completes its execution, you have to declare that builder is final > otherwise it will be cleaned up with any other local variables because > it's no longer in scope. Actually the anonymous class object still has a reference to builder so it won't be "cleaned up" anyway. The final keyword has nothing to do with garbage collection. -- 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