在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;
}
}