Date: Sat, 31 Oct 98 14:27:11 JST From: SHUDOH Kazuyuki Subject: [JavaHouse-Brewers:21091] timing of class initialization To: java-house-brewers at java-house.etl.go.jp (JavaHouse Brewers ML) Message-Id: <199810310524.OAA25946@cafe.muraoka.info.waseda.ac.jp> 首藤です。 Java 言語自身とか、Java 仮想マシン(JVM) 関連の話題が少なくて悲しい 今日この頃です。 Java 言語の仕様、および JVM に興味のある方向けのパズル :) です。 Kaffe(*1), Japhar(*2), TYA(*3) の ML に投げられた おもしろいメイルです。 (*1) フリーの Java 仮想マシン http://www.transvirtual.com/ (*2) フリーの Java 仮想マシン http://www.japhar.org/ (*3) JDK,JRE/Linux 用 JIT コンパイラ http://tya.home.ml.org/ 添付するプログラム (InitTest.java) の挙動が、 JVM の種類でけっこう変わります。 これに関する Java 言語の仕様は、 Java Language Specification (JLS) の 12.4.1 にあります。 どの挙動が仕様として正しいのか、 挙動が違う JVM はなぜその挙動を示すのか、 なかなか興味深いです。 残念ながら、shuJIT も JLS と違う挙動を示すことが判明… SHUDO Kazuyuki/首藤一幸 私をたばねないで あらせいとうの花のように shudoh at muraoka.info.waseda.ac.jp ===== Message-ID: <3636F96F.52471AB8@pg.gda.pl> Date: Wed, 28 Oct 1998 12:01:03 +0100 From: Artur Biesiadowski Subject: Initialization test I'm sorry if you will get multiple copies of that mail. Attached is test for class initialization. I know that it might be hard to conform to it, but specs say so. I know for sure that tya fails this test, I haven't checked kaffe and japhar, because I do not have them at hand. Feel free to include it into your regression testing if you do not have something similar yet. Artur ===== public class InitTest { static boolean passed = true; public static void main(String argv[] ) { int x; if ( argv.length > 10 ) { x = Class1.field; Class1.field = 5; Class3.runMe(); } else { Class2.field = 5; Class2.runMe(); } x = Class4.finalfield; if ( passed ) System.out.println("PASSED"); else System.out.println("FAILED"); } } class Class1 { static int field; static { System.out.println("Class1 clinit - WRONG(static access)"); InitTest.passed = false; } } class Class2 { static int field; static { System.out.println("Class2 clinit"); } static void runMe() {} } class Class3 { static { System.out.println("Class3 clinit - WRONG(static mcall)"); InitTest.passed = false; } static void runMe() {} } class Class4 { final static int finalfield = 5; static { System.out.println("Class4 clinit - WRONG(final static access)"); InitTest.passed = false; } }