Posted By:
Aad_Nales
Posted On:
Saturday, November 5, 2005 04:11 AM
I know that you can put a block of code in a class that 'runs' before the first initialization. pretty much like: Class Test { { System.out.println("init block"); { public Test(){ } } But i found myself in a situation in which I wanted to use the same technique before any static use of a class. Which I resolved like this: Class Test { private static int testValue = -1; static{ testValue = 2; { public Test(){ } static void writeTestValue(){ System.out.println("test value is: "+testValue); } } It compiles a
More>>
I know that you can put a block of code in a class that 'runs' before the first initialization. pretty much like:
Class Test {
{
System.out.println("init block");
{
public Test(){
}
}
But i found myself in a situation in which I wanted to use the same technique before any
static
use of a class. Which I resolved like this:
Class Test {
private static int testValue = -1;
static{
testValue = 2;
{
public Test(){
}
static void writeTestValue(){
System.out.println("test value is: "+testValue);
}
}
It compiles and runs and seems to do the job. But I can not find out if this is just 'lucky' or if it is legal. Any folks out there who wish to offer me their opinion. TIA,
Aad Nales.
<<Less