Static field initialization timing
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Monday, August 5, 2002 07:36 AM

Currently, I am very interested in DCL problem and related java feature. During my invesitigation, I found something which is explained differently by a few experts. They have different view point about the time when the static field is initialized. See the following code. public class Singleton { private static final Singleton fSingleton = new Singleton(); private Singleton() { } public void foobar() { System.out.println("test method"); } } As you know, this is generally accepted thread-safe singleton which is accepted by java thread experts. Some of articles which   More>>

Currently, I am very interested in DCL problem and related java feature.


During my invesitigation, I found something which is explained differently by a few experts. They have different view point about the time when the static field is initialized.


See the following code.


			
public class Singleton {

private static final Singleton fSingleton = new Singleton();

private Singleton() {
}

public void foobar() {
System.out.println("test method");
}
}


As you know, this is generally accepted thread-safe singleton which is accepted by java thread experts.


Some of articles which explains the DCL problem says that the static field is not initilized until the first reference of fSingleton field.


The others says that the initilization of static field is completely up to the implementation of JVM.


I've read the JLS(java language specification) and concluded that the initilization timing is not specified by the JLS, so we can not know the exact static field initialization timing.


Am I right?

   <<Less

Re: Static field initialization timing

Posted By:   eimi_nos  
Posted On:   Monday, August 5, 2002 09:00 AM

You can see a time path of an initialization process here by calling the static method [foobar()]:



class Singleton {
private static final Singleton fSingleton = new Singleton();
static{
System.out.print("In static. ");
System.out.println(System.currentTimeMillis());
}
private Singleton() {
System.out.print("Instantiated. ");
System.out.println(System.currentTimeMillis());
}
public static void foobar() {
System.out.print("test method. ");
System.out.println(System.currentTimeMillis());
}
}
About | Sitemap | Contact