dcsimg
Is this 'legal' Java
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

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

Re: Is this 'legal' Java

Posted By:   Almagest_FUTT  
Posted On:   Monday, November 7, 2005 07:23 AM

Re: Is this 'legal' Java

Posted By:   Christopher_Koenigsberg  
Posted On:   Sunday, November 6, 2005 05:46 PM

the use of a "static" block? it's "legal"
About | Sitemap | Contact