Calling a function again better than assigning it to variable [duplicate]


This question already has an answer here:

<ul><li> <a href="/questions/1923795/java-method-invocation-vs-using-a-variable" dir="ltr" rel="nofollow">Java Method invocation vs using a variable</a> <span class="question-originals-answer-count"> 14 answers </span> </li> </ul>

Is calling a getter function in class that does not computation better than assign value to a new variable?


Class Test{ private AnotherClass a; public AnotherClass getA(){ return a; } }

Is it better to keep calling getA() in a same function or todo AnotherClass var1 = getA(); then keep using var1 in that function.

I want to understand memory vs performance & how can we balance it.

Edit: Usage example: Consider another class being list

AnotherClass var1 = test.getA(); if(var1!=null){ for(List var : var1){ // do something } or if(test.getA()!=null){ for(List var : test.getA()) // do something }

Note this kind of behaviour can be done multiple time in multiple places.


See this question: <a href="https://stackoverflow.com/questions/1923795/java-method-invocation-vs-using-a-variable" rel="nofollow">Java Method invocation vs using a variable</a>.

In general, you should code for readability rather than performance. The compiler may very well optimize away get() calls for you. If it doesn't, and this turns out to be an issue, you can always profile your code after it is finished to identify and eliminate bottlenecks. To start off, though, definitely go for readability.


  • Singleton class in Kotlin
  • Rhino.Mocks produces InvalidCastException when returning polymorphic object
  • RTE - wrap link based on condition
  • Creating webservice (JAX-WS) with functions which return custom types
  • Writing Category Instance for custom Lens
  • Calling a Classes' Method From Another Class in Flash
  • Accessing types defined inside package from java
  • How to join two tables and map the result to a case class in slick
  • How to get elements with a unique number from a json array in PostgreSQL?
  • How to manage state in JAX-RS?
  • How to style GCKUIMiniMediaControlsViewController in iOS?
  • how can I key rotate for google cloud storage service account?
  • Mongodb update() vs. findAndModify() performace
  • Creating JS objects in PHP with commas in between
  • It is possible use the same sql azure instance from two different cloud service of two different sub
  • Fragment gives me an error while inflating 1
  • Correct implementation of List Iterator methods
  • Row Count Is Returning the incorrect number using RaptureXML
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • output of program is not same as passed argument
  • JSON with duplicate key names losing information when parsed
  • Trying to switch camera back to front but getting exception
  • Javascript + PHP Encryption with pidCrypt
  • Jquery - Jquery Wysiwyg return html as a string
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • JTable with a ScrollPane misbehaving
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • unknown Exception android
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?