Блог пользователя rahul_1234

Автор rahul_1234, 9 лет назад, По-английски

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

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
9 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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.