aspose file tools*
The moose likes Beginning Java and the fly likes Initilization block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Initilization block" Watch "Initilization block" New topic
Author

Initilization block

MrKamal Joshi
Greenhorn

Joined: Jul 31, 2011
Posts: 24

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

Joined: Aug 16, 2005
Posts: 14273
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
MrKamal Joshi
Greenhorn

Joined: Jul 31, 2011
Posts: 24

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

Joined: Oct 27, 2005
Posts: 19722
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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

Joined: Jul 31, 2011
Posts: 24

Thanks everyone
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initilization block