I created the default “hello world” GWT application and noticed that it has the following default code:
/**
* Create a remote service proxy to talk to the server-side Greeting service.
*/
private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
I was wondering how this is possible since the declaration of GreetingService is:
@RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
String greetServer(String name) throws IllegalArgumentException;
}
I thought you weren’t able to instantiate Interfaces?
Thanks!
Well, for one thing, it’s not calling a constructor on an interface. This is perfectly valid Java, and in many Service Locator frameworks it would be fine. It’s just calling a method, which returns something implementing
GreetingServiceAsyncas far as the Java compiler is aware.The next thing to remember is that it’s not going to be executing as Java anyway. GWT is going to be translating all of the client code into JavaScript magically. All it’s got to know is what the remote path is (so it can know where to make the relevant service calls) and what the signatures are (so the Java actually has something to work with, and so it can validate you’re passing appropriate arguments to the remote service).
At execution time this isn’t going to be building a Java object at all… it’ll be doing something in JavaScript so that it can make the RPC… and then on the server side, you can have a real implementation of the interface listening for that request.