Does GWT.create () always create a new object in browser memory?

Let's pretend that:

VeryLargeObject o1 = GWT.create(VeryLargeObject.class(); VeryLargeObject o2 = GWT.create(VeryLargeObject.class(); ... VeryLargeObject o1000 = GWT.create(VeryLargeObject.class(); 

where VeryLargeObject is the GWT resource interface that extends com.google.gwt.i18n.client.Messages .

Will this code create 1000 new instances of the object in the browser? Or is GWT smart enough to detect that VeryLargeObject immutable and reuse its 1 instance each time?

EDIT: I found this in docs , but it's still not clear to me how to do this:

Using GWT.create (a class) to "instantiate" an interface that extends "Messages" returns an instance of an automatically created subclass that is implemented using message templates selected based on the language.

+5
source share
2 answers

Yes, GWT.create() will return a new instance each time. But a good generator will make it so that it can be optimized in compiled code.

One of the first things that the GWT compiler does is type tightening (rewrite the code to use the most appropriate class, in which case all use of your message interface will be replaced by the generated implementation), and then execute the static methods (unless dynamic dispatch is required , i.e. polymorphism is actually used).
For the I18N message interface, because the generated class has no state, and its constructor has no side effect, this means that instances can be optimized and only static methods are stored in code (when they are not later built in).
More “complex” cases (for example, client packages, CSS resources) will actually use the “static state”, so again you can optimize the instances themselves and, in the end, it doesn't matter if you created 1000 instances or shared only one.

+4
source

According to the GWT Javadoc, the create () method will return a new instance, so I doubt the same object

http://www.gwtproject.org/javadoc/latest/com/google/gwt/core/client/GWT.html#create(java.lang.Class)

+2
source

All Articles