Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What does the "static" block in following class does?

 
Pras Tiwari
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have static block as follows in my java class:-

public class SimpleWeb extends Thread {
static {
String image = "image/";
MIME_TYPES.put(".gif", image + "gif");
MIME_TYPES.put(".jpg", image + "jpeg");
MIME_TYPES.put(".jpeg", image + "jpeg");
MIME_TYPES.put(".png", image + "png");
String text = "text/";
MIME_TYPES.put(".html", text + "html");
MIME_TYPES.put(".htm", text + "html");
MIME_TYPES.put(".txt", text + "plain");
}
//other methods in class
}

What does this static block does??

Thanks in advance.

Pras
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

It guarantees that when your class is loaded, the member MIME_TYPES is filled with the values being set. It's just a way to soon initialize the MIME_TYPES member.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic