在非静态块:
{ // Do Something...}
每次构造类的实例时被调用。在静态块只被调用一次,当类本身初始化,无论该类型的有多少对象创建。
例:
public class Test { static{ System.out.println("Static"); } { System.out.println("Non-static block"); } public static void main(String[] args) { Test t = new Test(); Test t2 = new Test(); }}
打印:
StaticNon-static blockNon-static block
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)