| Author |
How initializer blocks help to share a block of code between multiple constructors?
|
Varuna Seneviratna
Ranch Hand
Joined: Jan 15, 2007
Posts: 164
|
|
http://java.sun.com/docs/books/tutorial/java/javaOO/initial.html In this page Under Initializing Instance Members there is:
The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.
What is meant by the above? [ December 15, 2008: Message edited by: Varuna Seneviratna ]
|
Varuna Seneviratna
|
 |
BV Boose
Ranch Hand
Joined: Jul 26, 2008
Posts: 33
|
|
It means that the code between the curly braces will be called by every constructor of the class. produces: I am an intializer block, somebody called me I'm constructor number 1 I am an intializer block, somebody called me I'm constructor number 2 I am an intializer block, somebody called me I'm constructor number 3 3 different constructors all called
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2552
|
|
Varuna, please consider making your subject line more descriptive. I can hardly make out anything of it. Read this to know why this is important. You can edit your post by clicking the .
|
SCJP, SCWCD.
|Asking Good Questions|
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2552
|
|
|
.. and while we are on the topic, you might also want to take a look at the order in which initialization blocks and constructors are invoked.
|
 |
 |
|
|
subject: How initializer blocks help to share a block of code between multiple constructors?
|
|
|