• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initializer Blocks  RSS feed

 
Arsen Babakhanyan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may need static initialisers, so you only initialise each static field once.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!