File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why are static initilizer blocks needed when we can put that code in static method?

 
Monica Shiralkar
Ranch Hand
Posts: 825
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whatever we can put in static initializer block, we can put in static method also. So why are static initializer blocks needed when we can put that code in static method.

thanks
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods have to be called, while static initializers are executed automatically when a class is loaded.
 
Piet Souris
Ranch Hand
Posts: 1099
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, in addition to Ulfs remark, when and from where would you invoke this
static initializer method?

Greetings,
Piet
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:And, in addition to Ulfs remark, when and from where would you invoke this
static initializer method?

You could put a call to it in a static initialiser block
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer a static method call for initialization over a static initializer block, when possible:

vs.

I think the former is slighty more readable, especially when there's initialization code like that for more than one variable, and because init blocks tend to clutter up variable declaration code.
Just a slight preference.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:I prefer a static method call for initialization over a static initializer block, when possible:


The second does have one advantage, which is that the block cannot be called more than once. (OK, calling the second one more than once doesn't really hurt in this case, but it's nice for the intention to be clear). I agree that the first is more readable, and that probably makes me prefer it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47274
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can combine the two.
 
Monica Shiralkar
Ranch Hand
Posts: 825
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 47274
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic