I have a method repeatedMethod
like this:
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
}
and I call the repeatedMethod
many times.
I would like to ask if it is wise to declare myString
as static final
outside that method like this:
public static final String s = "myString";
public void repeatedMethod() {
anotherMethod(s);
}
I think that when I do anotherMethod("myString")
, a new instance of String
is created. And since I do that many times, many instances of String
are created.
Therefore, it might be better to create only one instance of String
outside the repeatedMethod
and use only that one every time.