wood burning stoves 2.0*
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between static and not static intializer blocks