Back
I was looking over some code the other day and I came across:
static { ...}
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?
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.
31k questions
32.8k answers
501 comments
693 users