[antlr-interest] [OT] Static initialization in Java (was: Re: site back up)

Kay Röpke kroepke at classdump.org
Wed Aug 27 04:33:13 PDT 2008


Hi!

On Aug 26, 2008, at 9:19 PM, Dennis Benzinger wrote:

> I didn't find anything definitive about that in the language
> specification but I think because HomePage.class is an instance of  
> class
> Class<HomePage> (or just Class before Java 5) it only requires the
> initialization of class Class. Initialization of class HomePage is not
> required.


Might not be required, but a very simple test shows the change of  
behavior:

public class Foo {
	static { System.out.println("Foo init."); }
	public Foo() {}
}


public class Bar {
	static { System.out.println("Bar init"); }
	public Bar() {
		System.out.println("Foo's class is " + Foo.class);
	}
}

public class Main {
	public static void main(String[] args) {
		Bar bar = new Bar();
	}
}

Java 5:
classdump:init kroepke$ java -cp . Main
Bar init
Foo's class is class Foo


classdump:init kroepke$ /System/Library/Frameworks/JavaVM.framework/ 
Versions/1.4/Home/bin/java -cp . Main
Bar init
Foo init.
Foo's class is class Foo

Further poking around seems to indicate some compiler change between  
1.4 and 1.5 with regards to Foo.class. I bet it's connected to  
generics because in 1.5 the type is Class<Foo> and previously it was  
1.4. I can imagine that when constructing Class in 1.4 something  
triggered initialization while in 1.5 less work is done and that  
doesn't trigger it.
Using .class (which is syntax, not a field) doesn't seem to fit into  
the section of the spec you referred to, so I'm afraid that's not  
really helpful to understand this :(

cheers,
-k
-- 
Kay Röpke
http://classdump.org/








More information about the antlr-interest mailing list