public class TestClass{
private static final String CONST_STRING;
static{
CONST_STRING=getConstString();
}
private static String getConstString(){
StringBuilder str=new StringBuilder();
str.append("statement1");
str.append("statement2");
.
.
.
str.append("statementN");
return str.toString();
}
//Calling frequently
public static void display(){
System.out.print(getConstString());
//OR
System.out.print(CONST_STRING);
}
}
任何人请帮助我了解哪种方法更好? 1.将字符串存储在常量字段中 2.在方法
中初始化String