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 ]