rahul_1234's blog

By rahul_1234, 9 years ago, In English

For code: plz explain the output. I can't get how Test1 t1 = new Test1(10); is getting executed despite not in constructor.

  • Vote: I like it
  • +1
  • Vote: I do not like it

»
9 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Link. In short: constructor is not the only one who is called when constructing a new object in Java. Fields are initialized before calling the constructor.

If it's not the desired behavior, you can always not initialize the field (if it's an object, it will be initialized to null) and initialize it in constructor.