This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes do not understand code that creation and definition of a class are combined together. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "do not understand code that creation and definition of a class are combined together." Watch "do not understand code that creation and definition of a class are combined together." New topic
Author

do not understand code that creation and definition of a class are combined together.

albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 245
The following code is presented in a book.
I do not understand the code between the line A and B, specifically creation (by "new") and definition of a class are combined together.
I usually see code that the class is defined first, then it is created (by "new") later.
Is there a name for this style of coding?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

See...

http://download.oracle.com/javase/tutorial/java/javaOO/nested.html

Or you can google for "anonymous inner class".

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Typically, when a class is created, the keyword "new" is followed by a constructor call, and that's the end of the line.

An anonymous inner class adds a class body immediately after the constructor. Note that the end-of-line semicolon is still required!

The resulting instance is a type that has no name (and is therefore anonymous) but is a subclass of the named constructor. The reference is upcast to the constructor type. And due to polymorphism, the instance will behave according to the anonymous class definition.

(Note: Because the anonymous class is a subtype that is overriding methods, an abstract class or interface can be used as the supertype.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: do not understand code that creation and definition of a class are combined together.