Java Static fields

SendTrash

Platinum Member
Apr 18, 2000
2,581
0
76
Hello,

I was looking at some Java code and I noticed this code chunk:

static {
/* basically checks size of logfile, if bigger than 100k, then copy it as bak, and start again */
}

This is in the class file outside of methods or the constructor.

My question is, when does this static field run? When you instiate the object? When the Java JVM is loaded? When?

Thank you.
 

PrincessGuard

Golden Member
Feb 5, 2001
1,435
0
0
It's called a Static Initialization Block. It's used to initialize static variables since they exist regardless of whether or not the class is instantiated. It is executed when loaded by the JVM.
 

manly

Lifer
Jan 25, 2000
13,196
3,980
136
Static initialization blocks are invoked when the class is first loaded by the JVM (implying a one-time event).