Whether you can access static variables static environment?

static variables belong to the class, it is of value in all instances is the same as in Java. When the class is loaded into the Java Virtual Machine, will static variables are initialized. If you do not have examples of code attempts to access the non-static variables, the compiler will complain, because these variables have not yet been created, not with any instance of the association.

public class Test {
    @org.junit.Test
    public void test(){

        //Demo demo =new Demo();
        System.out.println(Demo.a);
        //System.out.println(Demo.b);//报错

        Demo demo =new Demo();
        System.out.println(Demo.a);//通过
        System.out.println(demo.b);//通过
    }
}

class Demo{
    static int a = 1;
    int b =2;
}

Guess you like

Origin www.cnblogs.com/kristse/p/static.html