• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why even use initialization blocks at all?

 
John M. Gabriele
Ranch Hand
Posts: 232
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can't you always just use constructors and calls to this()?
it seems like init blocks can lead to confusing situations.
 
Andy Ceponis
Ranch Hand
Posts: 782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think init blocks are used when you have alot of things to initialize at one time. It makes the code clearer to the reader if they can look in one place for all the initialized things instead of having to look through the code to find each individual one.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not with anonymous classes you can't!
Regards,
Manfred.
 
John M. Gabriele
Ranch Hand
Posts: 232
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm... i could still init a bunch of things right in the first
constructor i write and then put this() (if i wrote them in a
no-arg) calls in the other constructors.
someone perusing the code could see the this() call and then go
back and hunt for the constructor with the same signature as the
this() call to find the initializations...
maybe the init block is used to avoid this kind of "hunting"
(even tho it doesn't seem so bad to me...)?
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Manfred mentioned, annonymous classes cant have constructors and initialization blocks are the only way for them. So they are useful/necessary for that if nothing else.
 
John M. Gabriele
Ranch Hand
Posts: 232
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohh... thanks manfred.
i didn't understand your post and thought maybe you were
responding to andy's comment.
thanks for the heads-up randall.
 
Andy Ceponis
Ranch Hand
Posts: 782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now im confused. Whats an anonymous class?
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is an example of an annonymous inner class that implements ItemListener interface.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic