Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question

 
Soum Sark
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does
System.out.print("1")
compile when in a {} and not when outside. What purpose does the {} serve ?
class Q {
{
System.out.print("1");
}
static {
System.out.print("2");
}
Q() {
System.out.print("3");
}
}
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
{} denotes an instance initialization block. Please refer to JLS 8.6 Instance Initializers for detailed information.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic