Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use GWT.create() instead of new?

What is the difference between GWT.create(SomeClass.class) and new SomeClass()?

Why would you use one over the other?

like image 472
RodeoClown Avatar asked Feb 11 '10 05:02

RodeoClown


People also ask

What is GWT create?

Google Web Toolkit (GWT) is a development toolkit to create RICH Internet Applications (RIA). Here are some of its notable features − GWT provides developers option to write client side application in JAVA. GWT compiles the code written in JAVA to JavaScript code. Application written in GWT is cross-browser compliant.

What is deferred binding?

Deferred Binding is a fully generic mechanism for handling features that vary according to some context. Another classic example of Deferred Binding is Internationalization: the GWT Compiler uses Deferred Binding to generate a completely separate version of the application for each language.


2 Answers

GWT.create is used by the GWT compiler for deferred binding. Deferred binding is a feature of the GWT compiler that works by generating many versions of code at compile time, only one of which needs to be loaded by a particular client during bootstrapping at runtime.

You should only use the GWT.create for those cases that depend on this specific use case. For example when creating a RPC class: (MyServiceAsync)GWT.create(MyService.class). In all other cases use new.

For more information check the GWT page on Deferred binding: http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsDeferred.html

like image 99
Hilbrand Bouwkamp Avatar answered Nov 27 '22 04:11

Hilbrand Bouwkamp


GWT.create employs deferred binding work to around for the lack of reflection support.

According to the FAQ:

Deferred Binding is Google Web Toolkit's answer to Java reflection.

It's easiest to explain Deferred Binding by starting with a use case. Every web browser has its own idiosyncrasies, usually lots of them. (The sheer unmanageable number of them is the problem GWT was created to solve in the first place.) The standard Java way of dealing with idiosyncrasies would be to encapsulate the custom code into subclasses, with one subclass for each supported browser. At runtime, the application would use reflection and dynamic classloading to select the appropriate subclass for the current environment, load the class, create an instance, and then use that instance as the service provider for the duration of the program.

This is indeed what GWT does. However, the JavaScript environment in which GWT applications ultimately run simply does not support dynamic classloading (also known as dynamic binding.) You can certainly include code to support each browser in your generated JavaScript code, but to do so you must include support for all browsers is in the single application file. Why should an Opera user have to download code specific to Firefox, when there is no chance at all that she will ever need it?

Because dynamic binding is unavailable as a technique to GWT, GWT instead uses deferred binding. One way to think of this is as "dynamic class-loading that occurs at compile time instead of execution time." When the GWT Compiler compiles your Java application, it determines all the different "idiosyncrasies" that it must support, and generates a separate, tightly streamlined version of the application for that specific configuration. For example, it generates a different version of the application file for Firefox than it does for Opera.

like image 38
Robert Munteanu Avatar answered Nov 27 '22 03:11

Robert Munteanu