• 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 block

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still can't see why initializer blocks exist? Why not just initialize variables where they are declared or in the constructors?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yah! Could someone come up with a really good and meaningful example for this topic?
------------------
Antti Barck
It Solutions Consultant, NSD Oy
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to initialize a static array variable of the numbers 1 to 100. Using a static initializer block you can do it very easily.
For instance variables the thought process is the same, if you have a variable that is initialized through a complex calculation you may just create an initializer block to do it rather than include it in every constructor for the class.
The only example I can come up with quickly would be if you were making a class that represented a deck of cards. You could use initializer blocks to initialize each suit with its cards.

hope that gave you an idea of their value

Dave
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave,
That was really good example and did not even need actual snippet of code!
------------------
Antti Barck
It Solutions Consultant, NSD Oy
 
wei luo
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave, in your example, do you mean we can create a class with a static array of integers from 1 to 13, initialize it in a static block, then in constructors we need only deal with the suits? Then, is instance initializer block still the same useful?
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy!
I seems to remember reading something about anonymous inner classes... These cannot take constructors like ordinary decent classes, so the only way to supply initializer code to an object of an anonymous inner class is to use initializer blocks.
This is another good reason for initializer blocks, in addition to the one mentioned by Dave Vick. Then again, I guess you could always chain your constructors to get rid of the initializer block..?
//Kaspar
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wei
Yes that would work great.
I guess it would depend on how you were implementing it. I haven't done it so never gave it a lot of thought. I think you could create a Deck class that you could use to create a bunch of different Deck instances, then each would have its own instance array of the cards - so you could keep the array in order for each Deck. Then you'd use instance initializers.
Or if you were just creating a card game you might only have one deck so it would be a static array and you'd use static initializers.

Dave
 
wei luo
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks to everyone.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic