• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java code format help

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw some code like

int i=0;
{
......
.......
.......
function2();
}
{
.....
....
}

not sure why add { } for specific code snippet. It looks for me it works without {}, could somebody point me what is the purpose of {} for those code snippet?

Thanks,


Steve
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably just as a visual clue for the human reader of the code. Braces tie the code they surround together, making it obvious which parts belong together, and which are separate. They also make it easier to attach an "if" condition or a "for" loop if you need to later.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We don't really know the context of this code. Ulf's answer assumes it's inside a method (or inside a constructor or initialization block). In that case the braces don't do much other than group things for human readability, and restrict the scope of local variables. If you need to do this sort of thing, it's usually better to just create new methods for each of the blocks.

However, if the code shown is nested inside a class, but not inside a method (or constructor or initializer block), then the braces represent an instance initializer block. You can learn more about these in the Java Tutorial.
 
Steve Jiang
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply. The code is in the method of generated code, not in class. So I guess as you said, it is just for visual, not what I thought there is something special for the { }.

Thanks!

Steve
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generated code? I have an app which generates code, and I put in {} and () every now and again possibly redundantly. It has several advantages
  • It makes it easier to find where one is should one need to go back and automatically insert an "if" or a "for" as Ulf Dittmer suggests
  • It maintains precedence; I have to insert (current-> before a token and ) after; that maintains the precedence if it is followed by a dot operator

  • BTW: The code I am writing is in C, not Java; the -> operator allows access to a member of a pointer to a struct. No, I won't explain it any more here.
    [ March 07, 2007: Message edited by: Campbell Ritchie ]
     
    reply
      Bookmark Topic Watch Topic
    • New Topic