Is one of the following options better than the other? What are the performance considerations?
class HelloWorld {
String text = "Hello World";
public String getText() {
return this.text;
}
}
HelloWorld helloWorld = new HelloWorld();
// option A:
for (int i = 0; i < HUGE_NUMBER; i++) System.out.println(helloWorld.getText());
// option B:
String text = helloWorld.getText();
for (int i = 0; i < HUGE_NUMBER; i++) System.out.println(text);
I'm asking specifically about the case where (1) the getter function simply returns a property without performing additional calculations and (2) the property is never changed (there is no need to get a "current" version of it).