When one creates a "New GWT Remote Service" a nice App class is added to the RemoteService implementation. This App class template can be improved with the following version:
public static class App {
private static final MyServiceAsync _I = (MyServiceAsync) GWT.create(MyService.class);
static {
String url = GWT.getModuleBaseURL() + "package.name/MyService";
((ServiceDefTarget) _I).setServiceEntryPoint(url);
}
public static MyServiceAsync get() {
return _I;
}
}
This template has several advantages:
1. instance is never checked for null state. This is a bit faster, plus it removes the unnecessary 'synchronized' modifier.
2. initializer is in static {} block, which is run when the class is loaded for the first time.
3. Class Init code is deleted in GWT JS code after it has been run once.