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

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