Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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 (46k 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.

Related questions

0 votes
1 answer
asked Jul 29, 2019 in Java by Suresh (3.4k points)
0 votes
1 answer

Browse Categories

...