1

Here is example code:

class A {

   static {
       int a;
       class B {
       }
   }

   public static void main(String[] args){
       // cannot access class B and in a;
   }
}

I don't know what the static keyword in this context means. I declare an int variable and a class inside it. But I cannot use it inside class A or in the main method. I compile and it doesn't produce any errors. So, I think this type of declaration has some purpose.

pb2q
  • 58,613
  • 19
  • 146
  • 147
hqt
  • 29,632
  • 51
  • 171
  • 250

6 Answers6

7

This is a static initialization block. You can use this to collect initialization for static/class members.

Similarly you can have a non-static initialization block to initialize instance members for each new object:

class A
{
    static int a;

    private int b;

    // static/class initialization:
    static
    {
        // initialize class members
        a = 5;
    }

    // instance initialization:
    {
        // initialize instance members
        b = 5;
    }
}

This example is trivial, you could instead just initialize the variables in their declaration: static int a = 5, and in fact generally that would be clearer. But use an initialization block when the initialization is multi-step, or generally more complicated, for example, setting up a database connection.

For more examples, see: Initializing Fields from the java tutorials.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • Can you tell me why we cannot at static keyword for enum. for example : `public enum A{ static{} }`. thanks :) – hqt Sep 06 '12 at 19:48
  • it will notice error : ` insert identifier to complete EnumConstantHeader`. thanks :) – hqt Sep 06 '12 at 19:52
  • 1
    @hqt you _can_ use a static initializer in an enum, for instance this should work: `public enum A { X, Y, Z; static { /* ... */ } } – pb2q Sep 06 '12 at 19:56
  • Oh. so great :) But why we cannot put it on a top of enum ? – hqt Sep 06 '12 at 19:59
  • @hqt the enum constants must be defined first. – pb2q Sep 06 '12 at 20:12
2

The code inside the static {} block will be executed when the class (not an object of this class) is loaded for the first time. See this

pascalhein
  • 5,700
  • 4
  • 31
  • 44
1

It is referred as static block in Java. This is usually used for initialization purposes that your Class A might require

static blocks are executed when JVM loads this class. There can be many such blocks and they would be executed in the order of appearance

Vikram
  • 4,162
  • 8
  • 43
  • 65
1

This is a static initialization block. Here is Oracle Java SE documentation static initialization blocks.

In your example, int a is a local variable to the static initialization block. Here is another Stackoverflow post regarding local variables in static initialization blocks: What is the scope of variables declared inside a static block in java?. That is why you cannot access it in your main method.

Community
  • 1
  • 1
Philip Tenn
  • 6,003
  • 8
  • 48
  • 85
0

That definition is a static initializer block. It allows extra logic for initialization, in this case applied to static members.

You will be able to access whatever you initialize in the block if you call the class A constructor, because "the Java complier copies initializer blocks into every constructor". Check docs

Alfabravo
  • 7,493
  • 6
  • 46
  • 82
0

This is the static initializer block.

The reason that int a and class B cannot be accessed outside of the block is due to scoping. Similar to constructors and methods, variables declared within the scope of the initializer block are not accessible outside of the block.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • can you tell me, why we cannot have static block in enum. Thanks :) – hqt Sep 06 '12 at 19:57
  • Think of an enum as a more limited form of a class that cannot have a static initializer block (and cannot extend any other class). – Reimeus Sep 06 '12 at 20:15