Flow of creating instance using GWT.Create and UI-Binder

46 Views Asked by At

I was going through the code of our GWT application. And got stuck at below piece of code.

private static TestEditorUiBind uiBinder = GWT.create(TestEditorUiBind.class);

interface TestEditorUiBind extends UiBinder<Widget,TestEditorViewImpl> {}

These are the starting 2 lines of my class. And after this we are creating our GWT Widget using uiBinder.createAndBindUi(this);

I know that GWT.create will create an instance of TestEditorUiBind at run time.

But i don't understand why we have declare TestEditorUiBind interface on 2nd line and used that on 1st line in GWT.create? (I have checked that TestEditorUiBind is not declared anywhere else in the application)

Please correct me if i understood something wrong.

1

There are 1 best solutions below

0
Colin Alworth On

Due to how the Java language works, the order of the lines does not matter - a new type (enum, interface, class) declared within another type actually becomes its own type and doesn't need to wait to be declared within that owner type. It looks like these two lines of code both exist in TestEditorViewImpl.java - assuming so, the java compiler will actually create two compiled bytecode files:

  • TestEditorViewImpl.class
  • TestEditorViewImpl$TestEditorUiBind.class

The first line then just asks for an instance of TestEditorViewImpl$TestEditorUiBind, where ever it happened to have been declared.

So yes, this static field is likely the only place in the project which will use that type, since it is specific to binding the .ui.xml to this class. A singleton can be used, since this binder will have no state, but will just populate fields within TestEditorViewImpl based on what it found in the .ui.xml file.