0 votes
1 view
in Java by (2.6k points)

I was looking over some code the other day and I came across:

static {

    ...

}

Coming from C++, I had no idea why that was there. Its not an error because the code compiled fine. What is this "static" block of code?

1 Answer

0 votes
by (46.1k points)

It's a static initializer. It's performed when the class is stored (or initialized, to be accurate, but you normally don't discern the difference).

It can be conceived of as a "class constructor".

Remark that there are also instance initializers, which examine the same, without that they don't have the static keyword. Those are spread in addition to the code in the constructor when a new example of the object is formed.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...