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.

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Can you tell me why we cannot at static keyword for enum. for example : public enum A{ static{} }. thanks :)
it will notice error : ` insert identifier to complete EnumConstantHeader`. thanks :)
@hqt you can use a static initializer in an enum, for instance this should work: `public enum A { X, Y, Z; static { /* ... */ } }
Oh. so great :) But why we cannot put it on a top of enum ?
@hqt the enum constants must be defined first.
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

Comments

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

Comments

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.

Comments

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

Comments

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.

2 Comments

can you tell me, why we cannot have static block in enum. Thanks :)
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.