File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes why are static initilizer blocks needed when we can put that code in static method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why are static initilizer blocks needed when we can put that code in static method?" Watch "why are static initilizer blocks needed when we can put that code in static method?" New topic
Author

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

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 639
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
Marshal

Joined: Mar 22, 2005
Posts: 41621
    
  55
Static methods have to be called, while static initializers are executed automatically when a class is loaded.


Ping & DNS - my free Android networking tools app
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 533
    
    8
And, in addition to Ulfs remark, when and from where would you invoke this
static initializer method?

Greetings,
Piet
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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

Joined: Mar 10, 2008
Posts: 1761
    
    7

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.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4375
    
    8

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

Joined: Oct 13, 2005
Posts: 38509
    
  23
You can combine the two.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 639
thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why are static initilizer blocks needed when we can put that code in static method?