• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Curly braces

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic