在Java中,成員變量的初始化有以下幾種方式:
public class MyClass {
private int num = 10;
}
public class MyClass {
private int num;
public MyClass() {
num = 10;
}
}
public class MyClass {
private int num;
{
num = 10;
}
}
public class MyClass {
private static int num;
static {
num = 10;
}
}
public class MyClass {
private int num;
public void setNum(int value) {
num = value;
}
}