Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets & Beans

 
Bruno Collins
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic