Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Passing an object to a method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing an object to a method." Watch "Passing an object to a method." New topic

Passing an object to a method.

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
I have this question. How this code snippet is allowed to compile.
amethod(new someclass() {} );
How can we have 'Curly Braces' after the constructor name?.
Thank you..

SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
It's an anonymous inner class.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Erik Pragt
Ranch Hand

Joined: Sep 08, 2001
Posts: 125
And, as an add-on, this even works for Interfaces.
*spooky* !!
Trent DiBacco

Joined: May 24, 2002
Posts: 18
and as another add on, this works with anonymous arrays as well.

[ January 11, 2004: Message edited by: Trent DiBacco ]
I agree. Here's the link:
subject: Passing an object to a method.
It's not a secret anymore!