• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Initilization block

 
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."!!
 
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 180
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone
 
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic