This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Difference between static and not static intializer blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between static and not static intializer blocks" Watch "Difference between static and not static intializer blocks" New topic
Author

Difference between static and not static intializer blocks

sarath chala
Greenhorn

Joined: Sep 28, 2004
Posts: 2
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

Joined: Jun 23, 2005
Posts: 266
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between static and not static intializer blocks