我正在尝试初始化一个字符串数组,但它有一个错误。
public class Account{
private String[] account;
public Account()
{
account = {"A", "B", "C"};
}
}
有谁知道为什么会继续创建错误?
答案 0 :(得分:8)
在构造函数中使用的正确语法是
account = new String[]{"A", "B", "C"};
您尝试使用的快捷语法仅在声明时允许:
private String[] account = {"A", "B", "C"};
至于为什么区别,请参阅Why can array constants only be used in initializers?
答案 1 :(得分:0)
参考:Arrays constants can only be used in initializers error
另请参阅:Why can array constants only be used in initializers?
"如果要使用数组初始值设定项,则不能拆分声明和赋值。"