• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

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

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic