Recently I got into a discussion with my Team lead about using temp variables vs calling getter methods. I was of the opinion for a long time that, if I know that I was going to have to call a simple getter method quite a number of times, I would put it into a temp variable and then use that variable instead. I thought that this would be a better both in terms of style and performance. However, my lead pointed out that in Java 4 and newer editions, this was not true somewhat. He is a believer of using a smaller variable space, so he told me that calling getter methods had a very negligible performance hit as opposed to using a temp variable, and hence using getters was better. However, I am not totally convinced by his argument. What do you guys think?
-
16The JVM will most likey inline getter calls. You should code for human readability first, and then if there's a performance issue, look at optimization – Steve Kuo Dec 17 '09 at 19:06
-
2When in doubt about performance, measure it! It is not difficult, and will allow you to put such performance concerns into perspective. (Does it really matter if this method takes 0.00000001 seconds longer to execute?) – meriton Dec 17 '09 at 20:45
-
1Some solid arguments in favor of the temp variable: http://stackoverflow.com/a/19446785/1553851 – shmosel Jul 08 '16 at 22:37
14 Answers
Never code for performance, always code for readability. Let the compiler do the work.
They can improve the compiler/runtime to run good code faster and suddenly your "Fast" code is actually slowing the system down.
Java compiler & runtime optimizations seem to address more common/readable code first, so your "Optimized" code is more likely to be de-optimized at a later time than code that was just written cleanly.
Note:
This answer is referring to Java code "Tricks" like the question referenced, not bad programming that might raise the level of loops from an O(N) to an O(N^2). Generally write clean, DRY code and wait for an operation to take noticeably too long before fixing it. You will almost never reach this point unless you are a game designer.

- 62,186
- 18
- 105
- 157
-
4I can agree with this more. +100 Maintainability and legibility are far, far more important than performance. – Randolpho Dec 17 '09 at 19:43
-
18I agree with the spirit but not the letter of this. There are many things that go into performance; micro-optimizations are only a small part, and sometimes a necessary evil on performance-critical code. A good programmer chooses their algorithms and data-structures well as part of the planning, and SHOULD consider performance in general as part of the design process. – BobMcGee Dec 18 '09 at 00:32
-
3@BobMcGee I agree completely--but I differentiate between "Programming" and "Optimizing". Doing an insertion sort into an array list when you have a linked list available is my optimal example of not actually knowing how to program. – Bill K Dec 18 '09 at 18:12
-
-
1Sorry, I still disagree. Aside from Bad Programming (such as picking the wrong data structure) I think you should not ever consider speed over readability until you actually hit a performance problem. I can speed up clean code, It's much harder to clean up fast code. If performance was always critical, nobody would use Python (It's 10x slower than java out the gate) or Ruby (100x slower). – Bill K Mar 05 '19 at 23:51
Your lead is correct. In modern versions of the VM, simple getters that return a private field are inlined, meaning the performance overhead of a method call doesn't exist.

- 55,384
- 17
- 145
- 179
-
Erm, including non-final ones? How does the virtual machine know the referenced object is not of a subclass which has overridden the getter? Please provide a source for your claim. (To put in perspective, the few nano-seconds overhead of a function call hardly ever matters) – meriton Dec 17 '09 at 20:27
-
1The VM will use profiling information to discover if the call to the property always ends up in the same place - see http://en.wikipedia.org/wiki/Inline_caching – Ben Lings Dec 17 '09 at 20:44
-
1Inline caching is not done with profiling information - at least according to the article you linked. Even with such a technique, I think there will be an additional branch for the creation/testing of the inline cache, which is unnessessary for a field access. – meriton Dec 17 '09 at 20:52
-
2@meriton: Perhaps I shouldn't have been so general. "simple getters are almost always inlined by the JIT compiler". Better? BTW, here's a research talk on the subject from Sun: http://research.sun.com/people/detlefs/talks/rice-98-talk/inline-talk.html. Note the date... summer, 98. This has been around for a decade. – Randolpho Dec 17 '09 at 20:53
-
+1: That was interesting reading, thank you. Still, it would be wrong to say that an inlined getter will be as fast as field access, because inlining of virtual methods requires an additional check (type, method or yet something else) at runtime. However, since the getter is called at least once in either approach presented the OP, and such checks are conceivably coalesced by the JIT, there should indeed be no performance difference at all. – meriton Dec 17 '09 at 21:20
-
And caching the variable locally will require a memory allocation. Do you really want to out-think the compiler when it's improving every year--and at a net cost, not gain in readability? – Bill K Dec 17 '09 at 23:26
-
1I believe if you look at the byte code you will get stack allocation either way. Whether it is a fixed position on the stack or temporary stack to pass to the next thing... or just the increased stack usage form making extra get calls that might not be inlined. I think worrying about the performance of this is irrelevant in most cases, either way. Readability is the key and sometimes it is more readable to cache, and sometimes not. After all, it's better to call a method like getTheValueFromTheThingWithAReallyLongName() as few times as possible. ;) – PSpeed Dec 18 '09 at 00:43
-
@PSpeed: the bytecode may imply stack allocation, but the JIT compiler will (likely) inline the method invocation at runtime. – Randolpho Dec 28 '09 at 16:45
Don't forget that by assigning the value of getSomething() to a variable rather than calling it twice, you are assuming that getSomething() would have returned the same thing the second time you called it. Perhaps that's a valid assumption in the scenario you are talking about, but there are times when it isn't.

- 1,074
- 5
- 6
-
1Corollary: if you call getSomething() repeatedly, in general you cannot assume that the returned value is the same. Consider the result in something like `if ( null != getSomething()) { getSomething().doSomething(); }`. – Andy Thomas Aug 21 '14 at 18:37
It depends. If you would like to make it clear that you use the same value again and again, I'd assign it to a temp variable. I'd do so if the call of the getter is somewhat lengthy, like myCustomObject.getASpecificValue()
.
You will get much fewer errors in your code if it is readable. So this is the main point.
The performance differences are very small or not existent.

- 39,095
- 19
- 120
- 139
If you keep the code evolution in mind, simple getters in v1.0 tend to become not-so-simple getters in v2.0.
The coder who changes a simple getter to not-so-simple getter usually has no clue that there is a function that calls this getter 10 times instead of 1 and never corrects it there, etc.
That's why from the point of view of the DRY principal it makes sense to cache value for repeated use.

- 44,836
- 10
- 105
- 121
-
+1 for being the only person here to remember about DRY. If I'm sure the value will stay constant between calls, and the value has to be get from a long getter chain with unknown underlying abstraction (possibly computationally expensive), I think only insane person will call the getter 10x instead of using temp for it. Also, you can often use much shorter names for temp variables than for getters, avoiding `doSomethingComplexWith( ( getThisDamnedVariableOfSomeType() == null ) ? someValue : getThisDamnedVariableOfSomeType() )` code smell – Nov 02 '14 at 15:59
I will not sacrifice "Code readability" to some microseconds.
Perhaps it is true that getter performs better and can save you several microseconds in runtime. But i believe, variables can save you several hours or perhaps days when bug fixing time comes.
Sorry for the non-technical answer.

- 71
- 2
I think that recent versions of the JVM are often sufficiently clever to cache the result of a function call automatically, if some conditions are met. I think the function must have no side effects and reliably return the same result every time it is called. Note that this may or may not be the case for simple getters, depending on what other code in your class is doing to the field values.
If this is not the case and the called function does significant processing then you would indeed be better of caching its result in a temporary variable. While the overhead of a call may be insignificant, a busy method will eat your lunch if you call it more often than necessary.
I also practice your style; even if not for performance reasons, I find my code more legible when it isn't full of cascades of function calls.

- 66,391
- 18
- 125
- 167
-
1Source? (How can the compiler determine that the method calls will always return the same result, i.e. that code executed in the mean time did not - directly or indirectly - modify the state accessed by the getter?) – meriton Dec 17 '09 at 20:36
It is not worth if it is just getFoo()
. By caching it into a temp variable you are not making it much faster and maybe asking for trouble because getFoo()
may return different value later. But if it is something like getFoo().getBar().getBaz().getSomething()
and you know the value will not be changed within the block of code, then there may be a reason to use temp variable for better readability.

- 39,895
- 28
- 133
- 186
A general comment: In any modern system, except for I/O, do not worry about performance issues. Blazing fast CPUs and heaps of memory mean, all other issues are most of the time completely immaterial to actual performance of your system. [Of course, there are exceptions like caching solutions but they are far and rare.]
Now coming to this specific problem, yes, compiler will inline all the gets. Yet, even that is not the actual consideration, what should really matter is over all readability and flow of your code. Replacing indirections by a local variable is better, if the call used multiple times, like customer.gerOrder().getAddress() is better captured in local variable.

- 9
- 1
-
1I disagree STRONGLY. Yes, a good compiler will handle the small details, but totally ignoring questions of implementation efficiency is the sign of a lazy programmer. You can make an application unusably slow by choosing an O(n^2) algorithm where an O(n log n) one works, or forcing a lot of un-necessary string parsing (*cough* XML *cough*). – BobMcGee Dec 18 '09 at 00:41
The virtual machine can handle the first four local variables more efficiently than any local variable declared after that (see lload and lload_<n> instructions). So caching the result of the (inlined) getter may actually hurt your performance.
Of course on their own either performance influence is almost negligible so if you want to optimize your code make sure that you are really tackling an actual bottleneck!

- 81,643
- 20
- 123
- 127
Another reason to not use a temporary variable to contain the result of a method call is that using the method you get the most updated value. This could not be a problem with the actual code, but it could become a problem when the code is changed.

- 28,547
- 16
- 75
- 90
-
1Using the latest value may also be a problem, if you've just stepped into a block of code based on the evaluation of the previous getter. And now you want to do something to that value that you've just checked ... but it has changed. – crowne Feb 09 '10 at 10:55
I am in favour of using temp variable if you are sure about getter will return same value throughout the scope. Because if you have a variable having name of length 10 or more getter looks bad in readability aspect.

- 314
- 3
- 12
-
-
I don't think performance is affected by doing so. If you are working on very critical project in which a nano second can also affect the performance then its your choice. – Rhushikesh Chaudhari Jul 01 '15 at 11:12
I've tested it in a very simple code :
created a class with a simple getter of an int (I tried both with final and non-final value for Num, didn't see any difference, mind that it's in the case num never change also...!):
Num num = new Num(100_000_000);
compared 2 differents for loops:
1: for(int i = 0; i < num.getNumber(); ++i){(...)} 2: number = num.getNumber(); for(int i = 0; i < number; ++i){(...)}
The result were around 3 millis int the first one and around 2 millis in the second one. So there's a tiny difference, nothing to worry about for small loops, may be more problematic on big iterations or if you always call getter and need them a lot. For instance, in image processing if you want to be quick, don't use repetively getters I would advise...

- 33
- 6
I'm +1 for saving the variable. 1) Readability over performance - your code is not just for you. 2) Performance might be negligible but not all the time. I think it is important to be consistent and set a precedent. So, while it might not matter for one local variable - it could matter in a larger class using the same value multiples times or in the case of looping. 3) Ease of changing implementation/ avoiding DRY code. For now you get the value from this one place with a getter and theoretically you use the getter 100 times in one class. But in the future - if you want to change where/how you get the value - now you have to change it 100 times instead of just once when you save it as an instance variable.

- 1