Questions related to initialization block. Their order of execution in case of multiple blocks, their default values etc
Questions tagged [initialization-block]
11 questions
308
votes
14 answers
Static Initialization Blocks
As far as I understood the "static initialization block" is used to set values of static field if it cannot be done in one line.
But I do not understand why we need a special block for that. For example we declare a field as static (without a value…

Roman
- 124,451
- 167
- 349
- 456
130
votes
4 answers
What is the difference between init block and constructor in kotlin?
I have started learning Kotlin. I would like to know the difference between init block and constructor.
What is the difference between this and how we can use this to improve?
class Person constructor(var name: String, var age: Int) {
var…

Samir Bhatt
- 3,041
- 2
- 25
- 39
127
votes
10 answers
What is an initialization block?
We can put code in a constructor or a method or an initialization block. What is the use of initialization block? Is it necessary that every java program must have it?

Sumithra
- 6,587
- 19
- 51
- 50
113
votes
10 answers
Use of Initializers vs Constructors in Java
So I've been brushing up on my Java skills as of late and have found a few bits of functionality that I didn't know about previously. Static and Instance Initializers are two such techniques.
My question is when would one use an initializer instead…

Inertiatic
- 1,223
- 2
- 9
- 8
87
votes
2 answers
Static block vs. initializer block in Java?
Possible Duplicate:
Static Initialization Blocks
Consider the following code:
public class Test {
{
System.out.println("Empty block");
}
static {
System.out.println("Static block");
}
public static void…

Anshu
- 7,783
- 5
- 31
- 41
4
votes
3 answers
Java Instance Initialization Block and instance variables
In context to my previous question Java classes and static blocks what if I changed my code from static block and variables to normal Instance Initialization Block and instance variables. Now how would the code be executed?
class extra3 {
public…

Shashank Agarwal
- 1,122
- 1
- 8
- 24
3
votes
0 answers
when does a static initializer block executes in java?
public class Parent {
public static int y=10 ;
}
public class Child extends Parent {
static {
y=20 ;
}
public static void main(String[] args) {
System.out.println(Child.y); // output = 20
}
}
public class Test…

Tohidur Islam Mondal
- 31
- 1
2
votes
4 answers
Instance initialization block and subclasses
I'm getting confused about when the instance initialization block should run.
According to Kathy Sierra's book:
Instance init blocks run every time a class instance is created
So, consider having two classes: a parent and a child, according to…

a.u.r
- 1,253
- 2
- 21
- 32
1
vote
1 answer
Why does encasing variable initialization into an initialization block allow initialization before declaration?
Consider the following code:
class New {
id = 2;
int id = 7;
}
Obviously it won't compile as we attempt to initialize an undeclared variable.
Encasing the statement into an initialization block, however, makes it compile…

John Allison
- 966
- 1
- 10
- 30
0
votes
4 answers
How to break out initialization block?
I have a class looks like this
class Some {
private enum Inner {
}
}
And I'm trying to find the Inner class in a initialization block of my test class.
class SomeTest {
private static final Class> INNER_CLASS;
{
for…

Jin Kwon
- 20,295
- 14
- 115
- 184
0
votes
1 answer
Can static class contains instance initialization block?
While reading Java I came across one question:
Can a static class contains instance initialization block?

Vaibhav Jagtap
- 9
- 1