Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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:
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());
Should not
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.
Reply With Quote
  #2 (permalink)  
Old May 25th, 2011, 02:52 PM
Authorized User
Points: 123, Level: 2
Points: 123, Level: 2 Points: 123, Level: 2 Points: 123, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2010
Posts: 18
Thanks: 0
Thanked 6 Times in 3 Posts

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.
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 11:59 PM.

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