18

What does static succeeded only by two curly brackets means? Sorry for the beginner question. I tried to look for a tutorial on it, but couldn't find one. This use of static as: static {}, is not clear for me. I found it being used by others heredeveloperWorks. I thought it may be a multiline, or group, modifier and tried the code below, but other type modifiers give errors not like static.

public class MyClass {
private volatile int v1=0;
private final int v2=0;
private static int v3=0;
static {             <----- No error here.
    int i1=0;
    String s1="abc";
    double d1=0;
};
final {              <----- Error here.
    int i2=0;
    String s2="abc";
    double d2=0;
};
volatile {           <----- Error here.
    int i3=0;
    String s3="abc";
    double d3=0;
};
}
Student
  • 339
  • 2
  • 11
  • Along with a static initialiser block, you can also initialise instance variables using {} alone without the static keyword. – JamesB Jan 24 '15 at 19:24
  • if you use it like you have it makes no sense. static initializer block would generally be used to initialize static members, for example `static int i; static {Obj obj = someObject(); i = obj.generateIValue(); }`, or perform other static initialization – eis Jan 24 '15 at 19:24
  • Can you give the similar question link, please? @Alexis King. – Student Jan 24 '15 at 19:29
  • Thanks @Alexis King for providing the link [particular usage of static](http://stackoverflow.com/questions/335311/static-initializer-in-java). Is it recommended to delete my question? – Student Jan 24 '15 at 19:50

1 Answers1

20

It is a static initializer block, which is used to initialize static members of the class. It is executed when the class is initialized.

Your example :

static {            
    int i1=0;
    String s1="abc";
    double d1=0;
};

makes no sense, since it declares variables that are only in scope until the execution of that block is done.

A more meaningful static initializer block would be :

static int i1;
static String s1;
static double d1; 

static {            
    i1=0;
    s1="abc";
    d1=0;
};

This example still doesn't justify using a static initializer, since you can simply initialize those static variable when you declare them. The static initializer block makes sense when the initialization of static variables is more complex.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • But! Why not able to use volatile, as example, in similar way? – Student Jan 24 '15 at 19:25
  • 1
    @javaStudent do you know what volatile does? – wvdz Jan 24 '15 at 19:28
  • Because it makes no sense. `static {}` doesn't mean what's inside is declared as static, but that code will be executed when the class is loaded. `volatile {}` wouldn't make any sense. – m0skit0 Jan 24 '15 at 19:28
  • 1
    @javaStudent The static initializer block is not used to declare static variables. It is used to initialize them. To declare static variables, each one requires its own static modifier, which is the same with volatile, final, etc... – Eran Jan 24 '15 at 19:29
  • 1
    @javaStudent You can say that, though there's a similar construct that looks exactly the same but without the static keyword, which is called instance initializer block. It is executed when an instance of the class is created, before the code in the consructor is executed. – Eran Jan 24 '15 at 20:06