Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does "object.new" work? (Does Java have a .new operator?)

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?

like image 255
chickeninabiscuit Avatar asked May 19 '10 05:05

chickeninabiscuit


People also ask

Which operator is used to create an object in Java?

Instantiation: The new keyword is a Java operator that creates the object. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

Which of the following would you use to instantiate a class in Java a new operator B class for name name?

forName("your class name"). newInstance() is useful if you need to instantiate classes dynamically, because you don't have to hard code the class name to create an object.

How do you instantiate a class in Java?

Java provides the new keyword to instantiate a class. We can also instantiate the above class as follows if we defining a reference variable. We observe that when we use the new keyword followed by the class name, it creates an instance or object of that class.

What is class object in Java?

A Class object is an instance of Class (java.lang.Class). Below quote taken from javadoc of class should answer your question. Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader ...


2 Answers

They're inner (nested non-static) classes:

public class Outer {   public class Inner { public void foo() { ... } } } 

You can do:

Outer outer = new Outer(); outer.new Inner().foo(); 

or simply:

new Outer().new Inner().foo(); 

The reason for this is that Inner has a reference to a specific instance of the outer class. Let me give you a more detailed example of this:

public class Outer {   private final String message;    Outer(String message) {     this.message = message;   }    public class Inner {     private final String message;      public Inner(String message) {        this.message = message;     }      public void foo() {       System.out.printf("%s %s%n", Outer.this.message, message);     }   } } 

and run:

new Outer("Hello").new Inner("World").foo(); 

Outputs:

Hello World 

Note: nested classes can be static too. If so, they have no implicit this reference to the outer class:

public class Outer {   public static class Nested {     public void foo() { System.out.println("Foo"); }   } }  new Outer.Nested.foo(); 

More often than not, static nested classes are private as they tend to be implementation details and a neat way of encapsulating part of a problem without polluting the public namespace.

like image 188
cletus Avatar answered Oct 09 '22 04:10

cletus


BookListUpdaterCallback and StoreOrderCallback are inner classes of BookStore.

See The Java Tutorial - http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html and http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html

like image 44
Greg Case Avatar answered Oct 09 '22 03:10

Greg Case