• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initializer Blocks

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i was thinking that when we create a new object it is created by constructor.
But now i am totally confused.

if we have a class like this :



it will work properly and even more it will be executed before constructor.
how it can be that the object is not created yet, but we can assign to its object variable any value ?

this idea of initializer blocks are very strange for me.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The constructor doesn't create the object. Creation is done by the runtime environment. The constructor is then used to initialise the state - but as you've noted it isn't the only way to initialise the state. Initializer blocks (and simple assignments on the same line as the declaration) can also be used. But the object is created before any of them run.

Think about this simple example of one constructor calling another:
Here two constructors get called. If the constructor did create the object, then wouldn't that create two new objects? But instead only one is created, and then both constructors act on it.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arsen Babakhanyan wrote:this idea of initializer blocks are very strange for me.


Probably because they are very rare. In general, you should initialize member values either directly, or in a constructor.

Static initializers, on the other hand:are much more common and, as the example indicates, usually used to initialize something that is quite complex, or that can't be initialized with a single statement.

Tip: If you do decide to write an initializer (and I'd think long and hard before you do), always keep it right next to the value it's initializing.

HIH

Winston
 
Arsen Babakhanyan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Arsen Babakhanyan wrote:this idea of initializer blocks are very strange for me.


Probably because they are very rare. In general, you should initialize member values either directly, or in a constructor.

Static initializers, on the other hand:are much more common and, as the example indicates, usually used to initialize something that is quite complex, or that can't be initialized with a single statement.

HIH

Winston



Actually i think that non-static initializer blocks are not adding any functionality into language, maybe they are just for convenience, everything can be done also without them.
Maybe i am wrong, but for now i don't have any example for it.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arsen Babakhanyan wrote:Actually i think that non-static initializer blocks are not adding any functionality into language, maybe they are just for convenience, everything can be done also without them.


And I'd say that's a pretty astute observation. Off the top of my head, I can't think of an occasion where you need one, and I've certainly never writtten one - and I've been writing Java for 12 years.

Winston
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may need static initialisers, so you only initialise each static field once.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:Off the top of my head, I can't think of an occasion where you need one, and I've certainly never writtten one - and I've been writing Java for 12 years.



You would use an instance initializer block in an anonymous inner class. Those things can't have constructors because they don't have names, so if you want to initialize the object's state in any way, you would have to use the instance initializer.

That being said, there still aren't many use cases. I have in the past used the "double braces" construction, which is an example:



In case it isn't obvious (which it isn't, which is why some people dislike this construction) that's an anonymous subclass of HashSet with an instance initializer block.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I have in the past used the "double braces" construction, which is an example:



In case it isn't obvious (which it isn't, which is why some people dislike this construction) that's an anonymous subclass of HashSet with an instance initializer block.



Double brace initialization, how is it I missed the existence of this. Just when you thought you had seen it all you learn something new in Beginning Java. Thanks Paul you made my day.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You would use an instance initializer block in an anonymous inner class...


Doh-h-h! Of course you would.

I really must get this Alzheimer's checked out...

Winston
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic