This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

brackets enclosure

 
Ranch Hand
Posts: 37
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic