搜尋此網誌

2012年4月9日 星期一

Java 靜態初始化區塊(Static initialization block)

When you want to initialize a static or instance variable in a more complicated way, you can use block to achieve this goal.
public class StaticInitBlockDemo {

static double[] arra = null;
static {
int size = 5;
array = new double[size];
for (int i = 0; i < array.length; i++) {
array[i] = Math.random();
}
}

public static void main(String[] args){
for(int i =0 ;i<array.length;i++ ){
System.out.println(array[i]);
}
}
}


Static Variable

沒有留言:

張貼留言