• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static block

 
Vallabhaneni Suresh Kumar
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the purpose of static block and what does it mean?

Thank you
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're probably familiar with how static variables are initialized the first time you reference a class (when the class is loaded). Static blocks are executed at the same time. That gives you a neat way to do a little more work than a single initial value. For example:

We could even move all those states into a static method and call it from the static block. Neat, huh?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic