This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlets & Beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets & Beans" Watch "Servlets & Beans" New topic
Author

Servlets & Beans

Bruno Collins
Greenhorn

Joined: Nov 30, 2001
Posts: 19
I am using a bean to store the result of a SQL query. When creating a bean to hold this info should I initialise the various fields in the declaration or is this overkill given?
eg:
public class MyBean {
private String mydata = "";
-OR-
public class MyBean {
private String mydata;
Thanks
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
If every field is always going to be filled by the query there is no need to initialize them. In any case I doubt you could measure the difference in execution speed.
Bill
Parag Shah
Greenhorn

Joined: Jan 23, 2002
Posts: 26
All Class variables are initialized to their default values. In case of Strings they are initialized to null. One reason to initialize the variables to "" is if you want to avoid getting any null values if you try to get a variable that has not been set. If that is not a problemthen you can leave them unitialized.
Parag


http://diycomputerscience.com
Do It Yourself Computer Science
Arun Boraiah
Ranch Hand

Joined: Nov 28, 2001
Posts: 233
hi,
If you are going to use all the data initialized or if all the fields are going to initialized on querying DB. Then assigning the empty string might help you to check some condition like checking stringname.equals("").
But it will create extra object which means extra server resource utilization. So better initialize to 'null' rather then to empty string.
-arun


Sharing is learning
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlets & Beans
 
Similar Threads
How to use <logic:iterate> over arraylist of a bean class
creating a sample java bean
Urgent: How to use Indexed Properties
Java Beans
JSF and DB2 interaction