ponder275 ponder275 - 1 month ago 11
Java Question

What is the correct way to initialize fields in Struts2?

I have a class which I am using to store data that I retrieve from a database which has objects as fields. I want to initialize the objects when the class is instantiated to avoid null pointer problems. I thought I read somewhere that it should not initialize the fields in the field declaration because it may cause problems for Struts (but I can't find the statement now), so I am initializing the fields in the constructor.

My question is: does it matter which way you do it? Or should you not do it at all and only put in the new objects after you instantiate the class? In other words should I define my class like this:

public class MenuView implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;

private ProjectInfo projectInfo;
private PartyInfo partyInfo;
private RequestTableInfo requestTableInfo;
private PartyRequestInfo partyRequestInfo;

public MenuView(){
projectInfo = new ProjectInfo();
partyInfo = new PartyInfo();
requestTableInfo = new RequestTableInfo();
partyRequestInfo = new PartyRequestInfo();
}


followed by getters and setters or like this.

public class MenuView implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;

private ProjectInfo projectInfo = new ProjectInfo();
private PartyInfo partyInfo = new PartyInfo();
private RequestTableInfo requestTableInfo = new RequestTableInfo();
private PartyRequestInfo partyRequestInfo = new PartyRequestInfo();

public MenuView(){ }


followed by getters and setters or like this:

public class MenuView implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;

private ProjectInfo projectInfo;
private PartyInfo partyInfo;
private RequestTableInfo requestTableInfo;
private PartyRequestInfo partyRequestInfo;

public MenuView(){}


followed by getters and setters and then instantiate it like this:

MenuView menu = new MenuView();
menu.setProjectInfo(new ProjectInfo);

Answer

I read somewhere that you should not initialize the fields in the field declaration because that may cause problems for Struts (but I can't find the statement now)

No, not that I know

so I am initializing the fields in the constructor.

You can, but you're not forced to. I never use constructors on actions (I almost never use constructors at all, since I'm using Java EE 6+ and CDI, and in constructors the @Injected objects have not been injected yet - I use a @PostConstruct method instead, when necessary), but that's up to you, it's not a rule.

My question is does it matter which way you do it?

No

Or should you not do it at all and only put in the new objects after you instantiate the class?

Struts2 will handle the nulls for you in the JSP. The only NullPointerExceptions you must handle are on the Java side, so just check for null, or instantiate the objects in the declaration, and don't worry about it anymore.

Just remember that Struts2 will need a no-arg constructor to instantiate beans with JSP values.

Comments