Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

brackets enclosure

 
Klament J. Kruoghst
Ranch Hand
Posts: 37
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I just saw in some code this:


Can someone explain to me the use of those brakets? I'm not sure if it's related to the Map or iterator declaration so that's why im putting it there, maybe it does maybe it doesn't
That doesn't look like an inner class and that doesn't look like an if/else statement, so what is it and why would someone want to use it.

Thanks,
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it serves any purpose, really. Although, IIRC, if you declare a new variable inside them, it will go out of scope once you leave that block.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only advantage that this style could have is what Fred already mentions: any variables that you declare inside the block go out of scope when the block ends, so you could use this to control the scope of the variables. However, using braces just for this purpose is not something that is commonly done in Java. I would avoid this style of programming. It is probably better to put the whole block in a separate method and call that method instead.


Better: Put the block in its own method.


Even better, but not the point of this topic: Use foreach style for-loops.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic