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();
As mentioned below in the comments, I realize a language could theoretically evolve to include most anything you want. I'm looking for a practical measurement of possibility. Would this conflict with other features? Is the implementation simple enough to work well with the JVM as it exists? And even, is it a good idea?
synchronized
keyword would work the same as if it was on the method. I imagine there would be some accomodation of more complicated construction methods. In my specific use case, due to the nature of the problem, with every request as it's own world, synchronized is pointless. – Nicole Feb 19 '11 at 2:09