最近做项目碰到这样的一个问题,发现JVM一个潜在问题,拿出来讨论一下!

import org.junit.Test;

import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

/**
* @author yaho
* @version 1.0 11-5-16,下午11:31
*/
public class StaticInitializerTest {
    private static int id = nextID();

    private static int nextID() {

        return identifier++;
    }

    private static int identifier = 0;
//private static int identifier = 1;

    @Test
    public void equality() throws Exception {
        assertEquals(id, nextID());
    }

    @Test
    public void unequality() throws Exception {
       assertThat(id,not(nextID()));
    }
}
程序运行不会出错,问题表明JVM初始化类变量失败,但不会报错。
把indentifier改成1时,equality运行不通过...
好像是JDK的一个Bug.其实应该抛错的.因为id=nextID();在id被赋值之前nextID()方法未定义....

你可能感兴趣的:(jvm,jdk,JUnit)