2

"The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors."

I have read this on this tutorial: http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

  1. I don't get it. What is the need of a constructor when an initializer block is there and doing the work of a constructor, in other words working as an alternative to a constructor?

  2. Oh but the constructor has to be invoked when creating the instance. OK! but then what is the need of an initializer block?

  3. "Therefore, this approach can be used to share a block of code between multiple constructors." Can somebody please give me an example? I mean I don't get it.

Thank you in advance.

user2882662
  • 535
  • 1
  • 6
  • 18
  • For 3) I think it's meant for something like this : ```class Foo { private int bar = 10; private int baz = 20; public Foo() { } public Foo(Foo anotherFoo) {} }``` You don't need to initialize the variables in both constructors. – Danstahr Oct 29 '13 at 11:25
  • [Use of Initializers vs Constructors in Java][1] [1]: http://stackoverflow.com/questions/804589/use-of-initializers-vs-constructors-in-java Hope it helps – Jabir Oct 29 '13 at 11:27

2 Answers2

0

I don't get it. What is the need of a constructor when an initializer block is there and doing the work of a constructor, in other words working as an alternative to a constructor?

Only constructors create objects but not the initializer blocks, hence constructors are required.

Oh but the constructor has to be invoked when creating the instance. OK! but then what is the need of an initializer block?

You can save repetition of code using initializer blocks when you have different constructors in your class.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • How do we save repetition of code? Do constructors include initializer blocks or do they implicitly call them? Yes I have read that java compiler copies all initializer blocks into every constructor. Does the java compiler do so implicitly and automatically without tell me, or do I have to write some code inside constructors so that initializer blocks get copied there. – user2882662 Oct 29 '13 at 11:33
  • @user2882662 Java compiler will do the job of copying the initializer code to constructors. You can simply relax and enjoy the work done by the java compiler for you. Looking into the generated code should help to give u more clarity. – Juned Ahsan Oct 29 '13 at 11:36
0

The only real need for initializer blocks is the static variant, which allows you to perform more complex initialisation of static final variables.

The difference between a constructor and an initializer block is in the fact that constructors construct the object, and initializer blocks are copied into every constructor. If you don't specify a constructor (even if you do specify an initializer block) the compiler will still generate a default constructor to handle object construction.

You can have different constructors executing different code, but all initializer blocks are executed regardless of which constructor is invoked. You can of course write a private method to the same as your initializer block and make your code more readable, but unlike initializer blocks you would need to manually invoke them from your constructor.

user268396
  • 11,576
  • 2
  • 31
  • 26