Is there any reason lazy initialization couldn't be built into Java?
- by Renesis
Since I'm working on a server with absolutely no non-persisted state for users, every User-related object we have is rolled out on every request.
Consequently I often find myself doing lazy initialization of properties of objects that may go unused.
protected EventDispatcher dispatcher = new EventDispatcher();
Becomes...
protected EventDispatcher<EventMessage> dispatcher;
public EventDispatcher<EventMessage> getEventDispatcher() {
if (dispatcher == null) {
dispatcher = new EventDispatcher<EventMessage>();
}
return dispatcher;
}
Is there any reason this couldn't be built into Java?
protected lazy EventDispatcher dispatcher = new EventDispatcher();