Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why we get away with this?

 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didn't know braces were allowed inside method declarations:




[edit]Delete excess whitespace. CR[/edit]
[ October 21, 2008: Message edited by: Campbell Ritchie ]
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you mean this?



Its creating an anonymous inner class that is a Runnable.

[ October 21, 2008: Message edited by: Paul Beckett ]
[edit]Delete excess whitespace. CR[/edit]
[ October 21, 2008: Message edited by: Campbell Ritchie ]
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kewl. i just googled it. thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50217
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done, but please spell cool cool not kewl: see this FAQ. And please set up your text editor to convert tabs to 4 spaces each; your original post was falling off the end of the screen with all the long tabs.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example you gave was indeed an anonymous inner class (that implements the interface Runnable). But besides that, it is possible to put arbitrary statements inside a method in a pair of { and }:

Note that in this example, the scope of the variable i is limited to the block.

Being able to create arbitrary blocks like this is an interesting feature, but I wouldn't use this in my own Java programs, because it's not something that's commonly done and it might confuse other developers that would read my code, and it's not something that's really very useful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic