views:

173

answers:

2

Do you bother initialising java bean values? Say, for example:

([g|s]etters omitted)

public class SomeClass {

    private String foo;

    private Date bar;

    private Baz someObject;

}

(Yes, this is a POJO being used as a bean rather than a Java Bean in the strictest sense)

In the empty constructor, do you initialise these members, or simply do nothing? Likewise, would Baz initialise it's members as well?

Or do you just leave them as null?

+1  A: 

It depends on the use case.

If I use properties as service dependencies, they should be initialized to operate properly (btw, Spring DI has handy way to do it).

If I use bean as part of domain model, it is usually illegal state to have some null property. It may not be initialized at startup, but I bother throwing IllegalStateException if some field is null during the business operation.

Maxim Ananyev
A: 

If at all possible, I initialize variables at declaration if they need to be initialized. These initialization statements actually wind up being called from the constructor.

I do generally leave them as null if there's not a compelling reason to initialize them.

skiphoppy