I came across this code today whilst reading Accelerated GWT (Gupta) – page 151.
public static void getListOfBooks(String category, BookStore bookStore) {
serviceInstance.getBooks(category, bookStore.new BookListUpdaterCallback());
}
public static void storeOrder(List books, String userName, BookStore bookStore) {
serviceInstance.storeOrder(books, userName, bookStore.new StoreOrderCallback());
}
What are those new operators doing there? I’ve never seen such syntax, can anyone explain?
Does anyone know where to find this in the java spec?
They’re inner (nested non-static) classes:
You can do:
or simply:
The reason for this is that
Innerhas a reference to a specific instance of the outer class. Let me give you a more detailed example of this:and run:
Outputs:
Note: nested classes can be
statictoo. If so, they have no implicitthisreference to the outer class:More often than not, static nested classes are
privateas they tend to be implementation details and a neat way of encapsulating part of a problem without polluting the public namespace.