Wrox Programmer Forums
|
BOOK: Professional Android 4 Application Development
This is the forum to discuss the Wrox book Professional Android 4 Application Development Reto Meier; ISBN: 978-1-1181-0227-5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android 4 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 6th, 2012, 11:37 AM
Registered User
 
Join Date: Feb 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question Why "final" in front of EditText?

in your sample ToDoList (page 111) we have

ListView myListView = (ListView) find....;
final EditText myEditText = (EditText) find...;

Why this EditText is made FINAL and not the ListView?

Maybe this is basic but I have done some research (with Schildt's book) and didn't find the answer

Thanks for your answer
 
Old April 21st, 2013, 02:17 AM
Authorized User
 
Join Date: Apr 2013
Posts: 14
Thanks: 0
Thanked 1 Time in 1 Post
Default

I also want to know more about this, this is the sole reason I registered on this website as I cannot find any other source on Google that explains this.
 
Old April 21st, 2013, 05:22 PM
Authorized User
 
Join Date: Jan 2013
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default "final" in front of EditText

i guess that the reason of final has to do with "threading", but not in chapter 4,
but in chapter 8's todolist.
So i think that in the code of chapter 4 the use of final is not necessary, but in chapter 8 it is just safer to do so, as an (asynchronous) content provider is used in that todo list with his own thread, and i can imagine that the access to EditText from which the string is used to create a new record in the database is only safe when EditText is declared final.
So it might be that the author created the code with a content provider before he simplified to the code of chapter 4, and has left the "final" attribute
 
Old April 22nd, 2013, 05:28 AM
Authorized User
 
Join Date: Apr 2013
Posts: 14
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Kees,

Thanks for responding so quickly, however I must say that my reason for questioning this is not because I do not know what the final keyword does but, simply put I am not sure if it has the same advantages as for Java compilers or if it as you just explained.

I posted this question on StackOverflow but I did not have enough points to include the pa4d tag. Here is the link: Are there (Performance) benefits of Views in Android that use the final keyword e.g final EditText?.

I am at Chapter 5 at the moment, I am going to read Chapter 8 so I can understand your explanation thoroughly before I post again. However, I think that it's best if the author himself comment on this to clarify since the final keyword has many advantages e.g full immutability for thread-safety and optimization by the compiler

Last edited by Android23Geek; April 22nd, 2013 at 08:19 AM..
 
Old April 22nd, 2013, 07:22 AM
Authorized User
 
Join Date: Jan 2013
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default "final" in front of EditText revisited

Hi,
My guess is not closer to the truth, as i checked Chapter_8_To-do_List with myEditText without the final, by declaring it as a class member in stead of a local variable, and it still run perfectly, i mean without runtime errors.
So the compiler error "Cannot refer to a non-final variable myEditText inside an inner class defined in a different method" seems to be the only reason to make it final. However i don't see that inner class here i must confess
Let's hope that Reto himself will clarify what the real benefits are!
 
Old April 22nd, 2013, 08:16 AM
Authorized User
 
Join Date: Apr 2013
Posts: 14
Thanks: 0
Thanked 1 Time in 1 Post
Default

I think I may have found the answer on StackOverflow:
link to possible answer.

We can't post pictures, if we could I would post a picture of where the inner class is and the method inside it. I deleted the final keyword and Eclipse showed me in
which inner class and method the problem originates. The inner class is View.OnkeyListener and the method is public boolean onKey. It would be wise to use
Chapter 4 To Do List Part 1 as I don't know how the code changes in Chapter 8.





Similar Threads
Thread Thread Starter Forum Replies Last Post
not like the book in "Chaper 11"-"Ex11_01" Louaih83 BOOK: Ivor Horton's Beginning Visual C++ 2010 0 April 2nd, 2012 09:38 PM
MSXSL gives error message for "for" inside "select" ilyaz XSLT 1 December 9th, 2010 05:02 PM
How to theme the "Browse" button of "FileUpload" control? varunbwj BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 October 14th, 2009 01:22 AM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 04:56 AM
Code not going as planned: "icicle" vs "savedinstancestate" joopthecat BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 3 May 3rd, 2009 03:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.