• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

{} inside a method.

 
Meg Adal
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote following program,it compiles and runs fine.But I was expecting compilation error for {}
I am not able to undestand why does Java allows to have {} inside a method?
Is it treated as Instance Initializer block?
 
Jon Dornback
Ranch Hand
Posts: 137
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's perfectly legal to have a code block within a code block. i don't think it is commonly used, but the main effect is to limit the scope.
the code below shows an example of how it works:
 
Ron Newman
Ranch Hand
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, empty code blocks are perfectly legal, and occasionally useful (for instance, to act as placeholders for future code).
 
Meg Adal
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic