-1

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?

clD
  • 2,523
  • 2
  • 22
  • 38
  • "Explain the output" is generally answered by "because that's what the code is written to do". What are you able to deduce about the behavior of this code from the output? What output did you expect and why? – David Dec 24 '20 at 15:19
  • @David I disagree. In this case, there are several things that might be tricky, for example, initialization order. There are things that could be explained. – Maksym Rudenko Dec 24 '20 at 15:20
  • It would help to find where exactly you are thinking wrong if you posted what output you expect. Also, check out the debugger. It will drastically change how you approach this kind of issue. – Maksym Rudenko Dec 24 '20 at 15:22
  • 1
    @MaksymRudenko Then the OP should specify that in the question. Something like "initialization order," rather than "Can someone explain to me the output of this code." – Spectric Dec 24 '20 at 15:22
  • Hey Guys, if you don't want to help, no problem, but don't judge me. I am new here, so I don't know much things. I don't understand this: { this.b1 = 2; this.b2 = 6; } Is this Constructor? Thanks – Nikola Stankovic Dec 24 '20 at 15:26
  • @NikolaStankovic that's called an [initialization block](https://stackoverflow.com/questions/3987428/what-is-an-initialization-block). Also, no-one's judging you. They're discussing the merits of your question, not yours personally. – Federico klez Culloca Dec 24 '20 at 15:28
  • Anyway see [this comment](https://stackoverflow.com/questions/3987428/what-is-an-initialization-block#comment42971390_3987586) in particular about why your code behaves the way it does. – Federico klez Culloca Dec 24 '20 at 15:30
  • @FedericoklezCulloca Thanks, I understand now. Too many people don’t like my question, so it became silly for me to ask questions. – Nikola Stankovic Dec 24 '20 at 15:32
  • Sometimes I don't know how to ask question in the right way. – Nikola Stankovic Dec 24 '20 at 15:34
  • @NikolaStankovic Yes, it would have been better had the question been improved, but the problems people had weren't with you, but rather procedural. – NomadMaker Dec 24 '20 at 16:09
  • @NomadMaker I understand, thank you for advice. – Nikola Stankovic Dec 24 '20 at 16:24

1 Answers1

1

The reason the code outputs b1 = 2, b2 = 10; is because of the values you are passing to the output stream

The simplified order of initilizastion is

  • static variables and static initializers in order
  • instance variables and instance initializers in textual order
  • constructors

So in your code you assign values in left to right order in which they appear textually in the source code for the class

int b1 = 5;

// instance initialization block
{       
    this.b1 = 2;
    this.b2 = 6;
}

int b2 = 10;

So the initialization int b1 = 5; and assignment this.b2 = 6; are essentially redundant due to later assignments.

thus values 2 and 10 are passed to System.out.println("b1= " + objB.b1 + " ,b2= " + objB.b2); which outputs:

b1 = 2, b2 = 10;

Java - 12.5. Creation of New Class Instances

clD
  • 2,523
  • 2
  • 22
  • 38
  • _"because of the values you are passing to the output stream", what do you mean with this? There is no output stream involved in this code. – Mark Rotteveel Dec 26 '20 at 12:37
  • @Mark Rotteveel `out` is Static member of `System` which is of type `PrintStream` to which the arguments are being passed or more technically a `StringBuilder append` as variables and concat is involved – clD Dec 26 '20 at 12:54
  • And that has no bearing on the actual problem, nor the reason for the result. – Mark Rotteveel Dec 26 '20 at 12:59
  • The OP says "Why does it print these values" it prints the values because they are being passed `PrintStream` and the reason those values are printed and not others is the way they were initialized and assigned – clD Dec 26 '20 at 13:01