Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initilization block

 
MrKamal Joshi
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HELP!!
Suppose we put a normal initilization block inside a method of a class.Then will code inside that initilization block will be copied to every 'constructor body' of that class or not??
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15490
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

No, the content of an instance initializer is not copied to the body of every constructor. However, instance initializers will always be executed when you create a new instance of the class, no matter which constructor you use to do that.

Please keep it down - there's no need to post with extra large text.
 
MrKamal Joshi
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Welcome to the Ranch!

No, the content of an instance initializer is not copied to the body of every constructor. However, instance initializers will always be executed when you create a new instance of the class, no matter which constructor you use to do that.

Please keep it down - there's no need to post with extra large text.

in this page <http://download.oracle.com/javase/tutorial/java/javaOO/initial.html> under "Initializing Instance Members" it is given that "The Java compiler copies initializer blocks into every constructor."!!
 
Rob Spoor
Sheriff
Pie
Posts: 20668
65
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler indeed does this. A small example as proof:
If I use JAD to decompile this class I get the following:
As you see both the initialization of x and the initialized block code are put inside both constructors.

Note that the parameter name is not stored in the byte code, so JAD uses a generic i for it.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mrkamal joshi wrote:Suppose we put a normal initilization block inside a method of a class...

In Java, wherever you can have a statement, you can also have a block of statements enclosed within braces. This copying you speak of, happens only when a non-static block of statements is used within the class body. In this case, the block of statements can be called an instance initialization block because it participates in the construction of an instance. However, when a block of statements is used inside a method, it is not an instance initialization block; it is just a block of statements. You can use this type of block to limit the scope of local variables i.e. the scope of a local variable declared inside a local block is limited to the local block.

 
MrKamal Joshi
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic