我有这样的方法repeatedMethod:
public static void repeatedMethod() {
// something else
anotherMethod("myString");
// something else
}
public static void anotherMethod(String str) {
//something that doesn't change the value of str
}
Run Code Online (Sandbox Code Playgroud)
我repeatedMethod多次打电话.
我想问一下myString,static final在这种方法之外声明是否明智是这样的:
public static final String s = "myString";
public void repeatedMethod() {
anotherMethod(s);
}
Run Code Online (Sandbox Code Playgroud)
我认为,当我这样做时anotherMethod("myString"),String会创建一个新实例.由于我多次这样做,String因此创建了许多实例.
因此,最好只创建一个String外部实例,repeatedMethod每次只使用一个实例.