To spell it out, this is one way of accomplishing it:
public class Whatever
{
private List<String> children;
public List<String> getChildren
{
if ( children == null ) { children = new ArrayList<String>(); }
return children;
}
...
}
Also, remember to write eg. addChild( String child )
in terms of the get
'er rather than directly accessing the field. And if you're really paranoid and/or in a threaded environment and the creation takes a long time, you might want to make the block synchronized
.