public class StaticInitDemo { static int i = 5; static { System.out.println("Static code i= "+ i++ ); } }为什么这个能把i加值 I don't know .
static定义的变量被所有对象共享,并且对所有实例只使用一个备份 static定义的方法不需要实例化就可以访问他如: public class Test{ public Test(){} public static test newInstance{return new Test();} public void printMsg(){System.out.println("test");} } 这是一个对Test实例化的例子 如果要的到Test的一个实例可以这样Test test=Test.newInstance()等同于Test test=new Test(); 如果要调用printMsg()可以这样 Test.newInstance().printMsg()。
static int i = 5;
static {
System.out.println("Static code i= "+ i++ );
}
}为什么这个能把i加值
I don't know .
static定义的方法不需要实例化就可以访问他如:
public class Test{
public Test(){}
public static test newInstance{return new Test();}
public void printMsg(){System.out.println("test");}
}
这是一个对Test实例化的例子
如果要的到Test的一个实例可以这样Test test=Test.newInstance()等同于Test test=new Test();
如果要调用printMsg()可以这样 Test.newInstance().printMsg()。