我已經在下面的代碼中初始化變量如下。可以像這樣初始化嗎?以這種方式初始化非靜態成員變量會有什麼問題嗎?
public class StaticInit {
int x = getInt();
String z = "Lucky Number " + processInt(x);
public static int getInt() {
int ret = 10;
System.out.println("ret- " + ret);
return ret;
}
public static int processInt(int toProcess) {
int toRet = toProcess/2;
System.out.println("toRet- " + toRet);
return toRet;
}
public static void main(String[] args) {
StaticInit sit = new StaticInit();
}
}
在某些情況下,並非所有的東西都可以在構造函數之外初始化。 - 例子? – 2013-03-27 11:10:54
如果構造函數具有將初始化變量的參數,例如。 – 2013-03-27 11:11:40