This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Java in General and the fly likes What is the main Use Static block  ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is the main Use Static block  ?" Watch "What is the main Use Static block  ?" New topic

What is the main Use Static block ?

Praveen Kumar
Ranch Hand

Joined: Nov 06, 2006
Posts: 133
We can write static blocks in java like

static {



Here we can initailise the varaibles before its construcor runs.

Do we have any another use of the Static block ?
Rob Spoor

Joined: Oct 27, 2005
Posts: 19908

Actually that code is run when the class is loaded. Even when there will never be any object created, the code will still run. What you are thinking about are initializer blocks, which are the same but without the static keyword.

Now these static initializers should be used to run code that are required for a class to function properly. That could be any code required.

One example is loading libraries:

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: What is the main Use Static block ?