Can someone explain the initialization order of my code when instantiating a new class using an instance initialization block
When I instantiate a new class of type B
and print the values of the fields objB.b1
and objB.b2
the code will output:
b1 = 2, b2 = 10;
Here is the code -
class B {
int b1 = 5;
{
this.b1 = 2;
this.b2 = 6;
}
int b2 = 10;
public B()
{
}
public static void main(String[] args)
{
B objB = new B();
System.out.println("b1 = " + objB.b1 + ", b2 = " + objB.b2);
}
}
Why does it print these values and not the other values that are assigned?