File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the main Use Static block ?

 
Praveen Kumar
Ranch Hand
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic