This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
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

Difference between static and not static intializer blocks

 
sarath chala
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A{
static {
System.out.println("static block");
}
{
System.out.println("not static block");
}
}
What is the difference between the above static block and not static blocks?
Thanks,
Sarath
[ August 12, 2005: Message edited by: sarath chala ]
 
Satish Chilukuri
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static block executes when the class gets loaded. A non-static block is executed when the class is instantiated.



if you execute B with only line 1, the output will be "static block" followed by "Hello". If you execute with only line 2 the output will be "static block" "not static block" "print".
[ August 12, 2005: Message edited by: Satish Chilukuri ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic