Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this a Method or some thing else?  RSS feed

 
Quang Pham
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I hapen to see code like this:
----------------------
static {
if (MyActionForm.getMyStaticField() == null){
MyActionForm.setMyStaticField("Mountain");
}
}
----------------------
This code is inside a class where all variables are declared.
Can any one tell me if this is a method or some thing else?
Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's called a "static initialization block". The code is executed once, when the class is loaded.

If you ever see one without the "static", it's just an "initialization block", and that code is prepended to all the class's constructors.
 
Quang Pham
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!