This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori 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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 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: 13018
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
I agree. Here's the link:
subject: Servlets & Beans
It's not a secret anymore!