Is there any reason lazy initialization couldn't be built into Java?
https://softwareengineering.stackexchange.com/questions/49559
-
31-10-2019 - |
Вопрос
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?
Нет правильного решения