File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets & Beans" Watch "Servlets & Beans" New topic

Servlets & Beans

Bruno Collins

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?
public class MyBean {
private String mydata = "";
public class MyBean {
private String mydata;
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
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.
Parag Shah

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.
Do It Yourself Computer Science
Arun Boraiah
Ranch Hand

Joined: Nov 28, 2001
Posts: 233
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.

Sharing is learning
jQuery in Action, 3rd edition
subject: Servlets & Beans
It's not a secret anymore!