1. I accept that I am under the control of a higher power (Muppet).

Java beginner question: static construct

Sorry, I haven't been reading any book on Java lately for paucity of time. Please educate me on this simple issue. What's this construct?

static
{
    /* some code */
}


What's it called?

What does it do? I can guess that it declares a section of code as static, as in class specific rather than instance specific, but I am not sure. Secondly, what is the implication of having such a construct?
Permalink Send private email jingalala jingalala ™ 
March 12th, 2007 1:46pm
It's called a static block and it is executed when the class is loaded. (not declared, or constructed, but loaded.)

Stick a System.out.println in there and notice when it runs.
Permalink zed 
March 12th, 2007 1:52pm
First guess, since I haven't done Java in awhile is ... it executes the block of code upon loading.

That's what my practice-less muscle memory is telling me.
Permalink Send private email strawberry snowflake 
March 12th, 2007 1:52pm
It's called a static block. You can see it as the equivalent of a constructor, but for the class and not the instance. Code in the static block gets executed only once when the class is first loaded before any instances are created, it's normally used for stuff like Database drivers that need to register themselves with the system when they're loaded or to retrieve an instance of a logging class that's used universally, to set some other class constants or log important messages like "Class: ... Version x.y.z successfully loaded".

Using static blocks is generally frowned upon unless they're absolutely necessary, so when in doubt and there's any other way to handle what you want to do, do it the other way. Not for any particular technical reason, but because smartypants self-important architect-types will be quick to pass judgement if you do.
Permalink Send private email a2800276 
March 12th, 2007 1:54pm
A lot of things like this are better not done declaratively. It's like html vs javascript/DOM. I would much prefer to hook code into the java runtime than try and declare everything using horrible syntax like this.
Permalink son of parnas 
March 12th, 2007 1:55pm
It seems like I remember it being featured prominently in singleton implementations.

I only had a single course in Java like seven years ago or something though.
Permalink Send private email JoC 
March 12th, 2007 1:55pm
I hear it's called a static block, and that it's executed when the class is loaded.
Permalink Send private email muppet 
March 12th, 2007 1:56pm
Go insult me in that other thread. I tried to incite you and frankly your ignorance (as in, ignoring me) is irritating and I could use the abuse.
Permalink Send private email JoC 
March 12th, 2007 1:57pm
That was an attempt to incite?  Dude, you seriously need work on your trolling skills.  I recommend that you start out gradually with the Martha Stewart forums and work your way UP to CoT.
Permalink Send private email muppet 
March 12th, 2007 1:59pm
"I would much prefer to hook code into the java runtime than try and declare everything using horrible syntax like this."

Can you explain what you mean by "hook code into the java runtime?"  Do you mean have a static method in the class, say, initializeThisClass()?
Permalink zed 
March 12th, 2007 2:00pm
Thank you for explaining it very nicely, everyone. I understood all your comments very clearly.


> Go insult me in that other thread. I tried to incite you and frankly your ignorance (as in, ignoring me) is irritating and I could use the abuse.

JoC, are you saying this to *me*?
Permalink Send private email jingalala jingalala ™ 
March 12th, 2007 2:03pm
> Do you mean have a static method in the class, say, initializeThisClass()?

Somewhere in the JVM there's some code that loads and initializes classes. I would like to just insert my own preinitialization code programmatically.
Permalink son of parnas 
March 12th, 2007 2:03pm
Prolly lazy initialization. You know, don't open db connections until some client really, really asks for db data.
Permalink Send private email strawberry snowflake 
March 12th, 2007 2:03pm
No, jinga, muppet. He sort of hit the mark now.

I wouldn't try to incite you, because I had more playful banter instead of being stalked in mind. :)
Permalink Send private email JoC 
March 12th, 2007 2:06pm
LMAO.
Permalink Send private email muppet 
March 12th, 2007 2:06pm
> I wouldn't try to incite you, because I had more playful banter instead of being stalked in mind. :)


Are you saying *this* to me?
Permalink Send private email jingalala jingalala ™ 
March 12th, 2007 2:08pm
No, no I am most definitely not.

*looks over shoulder*

I was only teasing you.
Permalink Send private email JoC 
March 12th, 2007 2:09pm
<apu voice>
  Are you speaking to me?  Are you speaking to me?  There's no one here but you, me, and the Slushee machine, so you must be speakin' to me.
</apu voice>
Permalink Send private email muppet 
March 12th, 2007 2:10pm
> I was only teasing you.

Now are you saying this to me?
Permalink Send private email jingalala jingalala ™ 
March 12th, 2007 2:10pm
<apu voice>
Are you speaking to me?  Are you speaking to me?  There's no one here but you, me, and the Slushee machine, so you must be speakin' to me.
</apu voice>

muppet, are you saying this to me?
Permalink Send private email jingalala jingalala ™ 
March 12th, 2007 2:14pm
We speak your name.
Permalink Send private email muppet 
March 12th, 2007 2:16pm
"Somewhere in the JVM there's some code that loads and initializes classes. I would like to just insert my own preinitialization code programmatically."

What class would things that you assign there be members of? You can be first in line if you put the static block in the class that has main.
Permalink zed 
March 12th, 2007 2:16pm
> We speak your name.


Whose name? JoC's or mine? Who are you saying this to now?
Permalink Send private email jingalala jingalala ™ 
March 12th, 2007 2:23pm
>>> Somewhere in the JVM there's some code that loads and initializes classes. I would like to just insert my own preinitialization code programmatically.

For better or for worse didn't the Java designers design the "static block" interface to accomplish just that?

What else would it look like? static block = preintialization code. Or is there some reason for importing classes to pass on parameters to the init block? Examples?
Permalink bleaty heartsheep 
March 12th, 2007 2:24pm
>Whose name? JoC's or mine? Who are you saying this to now?

Who are you talking to?
Permalink Send private email muppet 
March 12th, 2007 2:25pm
> Who are you talking to?


Who are you saying this to?
Permalink Send private email jingalala jingalala ™ 
March 13th, 2007 8:50am

This topic is archived. No further replies will be accepted.

Other topics: March, 2007 Other topics: March, 2007 Recent topics Recent topics