Is there any reason lazy initialization couldn't be built into Java?
Posted
by
Renesis
on Programmers
See other posts from Programmers
or by Renesis
Published on 2011-02-19T00:32:59Z
Indexed on
2011/02/19
7:32 UTC
Read the original article
Hit count: 386
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();
© Programmers or respective owner