Wrox Programmer Forums
|
BOOK: Professional Android 2 Application Development
This is the forum to discuss the Wrox book Professional Android 2 Application Development, 2nd Edition by Reto Meier; ISBN: 978-0-470-56552-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android 2 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 May 19th, 2011, 08:53 AM
Authorized User
 
Join Date: Nov 2009
Posts: 15
Thanks: 0
Thanked 1 Time in 1 Post
Default Is This Proper Java?

Is is acceptable in java to declare and type variables inside a while loop? This from Chapter 7 code:
Code:
if (toDoListCursor.moveToFirst())
    		do {
    			String task = toDoListCursor.getString(ToDoDBAdapter.TASK_COLUMN);
    			long created = toDoListCursor.getLong(ToDoDBAdapter.CREATION_DATE_COLUMN);
    			ToDoItem newItem = new ToDoItem(task, new Date(created));
    			todoItems.add(0, newItem);
    		} while (toDoListCursor.moveToNext());
    	aa.notifyDataSetChanged();
Should not
Code:
String task; long created; ToDoItem newItem;
appear before the loop and then just assignments made inside the loop? Seems like the loop will be creating hundreds of variables this way, unless Java somehow manages that automatically.
 
Old May 25th, 2011, 02:52 PM
Authorized User
 
Join Date: Jun 2010
Posts: 18
Thanks: 0
Thanked 6 Times in 3 Posts
Default

It's proper Java. Variables should always be declared within the "tightest" scope possible. They don't get re-declared each time through the loop -- one copy of each variable is allocated each time the containing method is called.





Similar Threads
Thread Thread Starter Forum Replies Last Post
proper use of xsl:key? ilyaz XSLT 5 December 10th, 2010 12:39 PM
Proper use of Arbiters scott.nortman BOOK: Professional Microsoft Robotics Studio ISBN: 978-0-470-14107-6 1 June 23rd, 2008 08:06 PM
tell me the proper free book name naveed77 Beginning VB 6 1 January 6th, 2007 11:31 AM
Proper Case IN Xsl Israr XSLT 8 April 4th, 2006 01:39 PM





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