public class myclass{
static{
//some statements here
}
//some variables declared here
//some functions defined here
}

- 208,112
- 36
- 198
- 279

- 1,525
- 3
- 12
- 8
-
possible duplicate of [What does the 'static' keyword do in Java?](http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-java) – Hans Olsson Aug 27 '10 at 12:04
-
THAT PARTICULAR QUESTION IS A POOR DUPE. It's only vaguely related. The linked questions in my answers are far more relevant. – polygenelubricants Aug 27 '10 at 12:29
-
2Another dupe: http://stackoverflow.com/questions/2420389/static-initialization-blocks – BalusC Aug 27 '10 at 12:36
5 Answers
It's a static
initializer. Analogously to the instance initializer (§8.6), you can use it to initialize your class
when it's loaded. It is NOT "invoked" explicitly; it is executed automatically when the class
is loaded, in textual order (static
initializer that occurs later in the text is guaranteed to be performed later during the initialization).
You can use a static
initializer to:
- Initialize some
static
fields - Perform some other one-time calculations, perhaps something requiring a
try-catch
block, logging events related to the loading of theclass
, making sure that Java's assertion is enabled, etc.
There are some caveats, e.g. a class
may be reloaded, and usually there are alternatives of writing it (e.g. refactoring into a private static
named method), but using a static
initializer is an option.
References
Related questions
These questions discuss various aspects of static
and instance initializers usage:
- Static initializer in Java
- How to handle a static final field initializer that throws checked exception
- Why is the order of declarations important for static initializers?
- Use of Initializers vs Constructors in Java
- Are Java static initializers thread safe?
An example usage: requiring that assertions are enabled
The following is the prescribed idiom in the Java Language Guide for Programming With Assertions to ensure that assertion is enabled at run-time:
Requiring that Assertions are Enabled
Programmers of certain critical systems might wish to ensure that assertions are not disabled in the field. The following static initialization idiom prevents a class from being initialized if its assertions have been disabled:
static { boolean assertsEnabled = false; assert assertsEnabled = true; // Intentional side effect!!! if (!assertsEnabled) throw new RuntimeException("Asserts must be enabled!!!"); }
Put this
static
-initializer at the top of your class.
By putting this snippet in a static
initializer for the class, the code will be one of the first thing executed when the class is loaded, before any instances of the class can be created. The code itself checks if Java assertion is enabled, and throws a RuntimeException
if it's not.
See also
Related questions

- 1
- 1

- 376,812
- 128
- 561
- 623
-
public class StaticTest { static{ System.out.println("This is the message in static block"); } public void normalMethod() { System.out.println("This is a message from method, After creating the object and invoking this method"); } public static void main(String[] args) { System.out.println("This is the message in Main, I didnot create the static block"); } } Output: This is the message in static block This is the message in Main, I didnot create the static block – Vijay Bhaskar Semwal Aug 27 '10 at 11:57
-
1@Vijay: You did write a `static` initializer block for the class, so when it is initialized, the code in the block will be executed. – polygenelubricants Aug 27 '10 at 12:16
Static blocks are executed when the class is loaded, so they are a good place to initialize static variables. Especially constants that require more logic than a one-liner may be initialized comfortably using a static block:
private static final String MY_CONSTANT;
static{
// read properties from file
MY_CONSTANT = properties.get("my.property.name");
}

- 292,901
- 67
- 465
- 588
Static blocs are used to initialize static values which might need real code to be initialized.
for example you want to do this
static boolean firstBoolean = staticMethodThatGetBoolean();
static boolean secondBoolean = staticMethodThatGetAnotherBoolean();
static int i = //if firstBoolean is true i = 1, if secondBoolean is true i = 2, if firstBoolean and secondBoolean are true i = 3;
Either you do something like this :
static boolean firstBoolean = staticMethodThatGetBoolean();
static boolean secondBoolean = staticMethodThatGetAnotherBoolean();
static int i = (firstBoolean && secondBoolean)?3:(secondBoolean)?2:(firstBoolean)?1:0;
or you create a static bloc to handle all of this.
Another example would be to handle exceptions thrown by staticMethodThatGetBoolean() or staticMethodThatGetAnotherBoolean()
Be careful, static bloc are meant to be initializer blocs in a static context. You must use them to initialize your static variables only. Even if you can invoke real code in here, this is a really really bad practice.

- 91,525
- 15
- 160
- 151
Static loop is executed, when the class is loaded. So without creating the object we can execute this loop.

- 1,525
- 3
- 12
- 8
-
1didn't take long to find the answer to your own question then :) – Philip Potter Aug 27 '10 at 11:57
-
1Why the downvote? Answering one's own question is acceptable, there's even a badge for it. A more complete answer was posted at roughly the same time, but that happens on SO. The point is to record lots of answers to questions. Now, the _second_ self-answer should have just been an edit to this one, but this one's fine. – David Aug 27 '10 at 12:04
-
1@David I agree (and didn't downvote) but being able to answer your own question after one minute implies that the OP was too lazy to Google the answer first – Sean Patrick Floyd Aug 27 '10 at 12:09
-
@David: I also didn't downvote, but the answer does say "loop" rather than "function" or "method", which could be confusing. – Douglas Aug 27 '10 at 12:16
-
@seanizer: There's merit to that point, ya. I've often been in the middle of answering a quick question only to find that the asker deleted it because he found the answer so quickly elsewhere. Though, barring excessive duplicates, I would argue in favor of posting good questions on SO even if the answer is already known, simply as a matter of recording the question and answer(s) for people to find later. – David Aug 27 '10 at 12:17
-
1@Douglas: Agreed. Seems to be simply a language barrier though. The superfluous comma bothers me even more :) – David Aug 27 '10 at 12:19
-
no it is only to dominate urself they are giving down vote as i know it i was expecting more better answer wht was my overview so i posted my answer – Vijay Bhaskar Semwal Aug 27 '10 at 13:06
public class Test {
static int a=3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4; System.out.println("a = " + a);
System.out.println("b = " + b);
}
public static void main(String args[]) {
meth(42);
}
}
output willl be--
Static block initialized.
a = 3
b = 12
x = 42
a = 3
b = 12

- 208,112
- 36
- 198
- 279

- 1,525
- 3
- 12
- 8
-
3did you have to provide 2 answers to your own question? How about deleting one? – Sean Patrick Floyd Aug 27 '10 at 11:59