Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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
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
 
Similar Threads
static block
Non-static initializer vs Constructor
static synchronized block
initialization blocks vs. constructors?
Initializer blocks