Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Curly braces

 
Rao Rekha
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could somebody kindly explain to me why a system.out.println("...") statement works without giving any error when specified within a pair of curly braces inside a class like the following.
0
class test{
{
System.out.println("...");
}
}
But it gives an error without the inner curly braces. Why?

Thank you
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rao Rekha:
Could somebody kindly explain to me why a system.out.println("...") statement works without giving any error when specified within a pair of curly braces inside a class like the following.
0
class test{
{
System.out.println("...");
}
}
But it gives an error without the inner curly braces. Why?

Thank you

The pair of curly braces forms an instance initializer. The body is processed just before processing the body of the constructor. If you put the word "static" in front of the curly braces, then it becomes a "static initializer" and would then be processed when the class is loaded.
If you would like to see more examples of instance initializers, then please see the single topic exam titled "constructors" on my web site.
[ September 09, 2002: Message edited by: Dan Chisholm ]
 
Rao Rekha
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Dan. I didnt know about this instance intializer before. It is only after seeing the questions at your web site that I got the doubt.
Anyway, your website has been very useful this way.
Thanks again.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic