Hey, guys, this guy is trying to make a VM and has a question.  I vaguely 
remember us running into something like this before with Japhar, and I am 
not sure how it was solved ... ?

>From: Vojta Filip <[EMAIL PROTECTED]>
>
>So with it I have else one problem - when String is loaded <clinit> is
>called. And in <clinit> of String is used Hashtable. And Hashtable has in
><clinit> manipulations with String. But String is not comletly initialized
>(<clinit> is not done)...
>

Hmm.  Reading the appropriate initialization procedure (JVM Spec sections 
2.16.4 and 2.16.5 at 
http://java.sun.com/docs/books/vmspec/html/Concepts.doc.html#19075), your 
statement is correct.

As far as I can tell, classes have to be initialized on first use, and 
according to the spec, and a constructor cannot actually be *started* unless 
its class is initialized.  (2.16.4 says that a class must be initialized as 
its constructor is invoked, and 2.16.5 says that if the class is 
uninitialized, you try to get a lock on the class before doing 
initialization.

I created a test case of JDK's behavior, though, and it does not do this, so 
perhaps I am reading the req's wrong?

Here is the test case:


public class A {
        static { System.out.println("Initializing A"); }
        A() { System.out.println("Constructing A"); }
        static B b = new B();
        public static void main(String[] args) { }
        static { System.out.println("Done initializing A"); }
}

class B {
        static { System.out.println("Initializing B"); }
        B() { System.out.println("Constructing B"); }
        static A a = new A();
        static { System.out.println("Done initializing B"); }
}


In JDK 1.2.2 this example works fine, and prints:


Initializing A
Initializing B
Constructing A
Done initializing B
Constructing B
Done initializing A


I suspect there may be an exception to the rule "you must synchronize on the 
object": if you are the Thread that already holds the lock on that Object, 
then perhaps Java should decide that it's OK to let you construct anyhow.  
This amounts to assuming that the people who set up the static initializers 
were not stupid and wouldn't use corrupted objects.

How is this handled in Japhar (I don't have that handy to test with)?

--John

______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

Reply via email to