• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What does this do? What is it's name?

 
Jerry Kilpatrick
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across the following code in one of the examples for the Swing Tutorial. I am not familiar with this syntax. What does it do? What is it's name?

Thanks,

Jerry



 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jerry Kilpatrick:
... I am not familiar with this syntax. What does it do? What is it's name? ...

I'm guessing that the syntax you're referring to is the anonymous class definition.

Basically, an anonymous class looks like a constructor call, but before the closing semicolon, a class definition is slipped in with braces. The result is a class that implicitly extends the class (or implements the interface) identified after the keyword "new."

Furthermore, the instance of this anonymous class is automatically upcast to the type it extends (or implements).

In this particular case, we have an anonymous class that extends JLabel, and the new instance is upcast to type JLabel.
[ November 30, 2005: Message edited by: marc weber ]
 
Jerry Kilpatrick
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc,

Exactly the answer I was looking for.

Thanks,

Jerry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic