Junior — Middle
Explain the process, starting from sending the request and ending with receiving the token
sobes.tech AI
Answer from AI
The initialization blocks in Java are code blocks that execute when an instance of a class is created or when the class is loaded.
There are two types:
-
Static initialization blocks (
static { ... }) — executed once when the class is loaded into memory. Used for initializing static variables or running code that should execute before any objects are created. -
Instance initialization blocks (
{ ... }) — executed each time a new object is created, before the constructor. They allow common initialization code for all constructors.
Example:
public class Example {
static int staticValue;
int instanceValue;
static {
staticValue = 10; // class load time initialization
}
{
instanceValue = 5; // object creation initialization
}
public Example() {
// constructor
}
}
Initialization blocks are useful when complex initialization logic is needed that cannot be expressed in variable declarations or constructors.