Best to explain with an example:
int RecordCount
{
return dataAdapter.getCount(); // database access
}
Now, each time this is called in the code, there will be a database lookup. But what if I assign RecordCount to a local variable like so:
int recordCount = RecordCount;
// multiple calls to recordCount follow...
I assumed that each time local variable recordCount was accessed, there would be a database lookup because recordCount is just a reference to RecordCount. However, my colleague believes that a lookup will only happen upon the initial assignment, and then recordCount can be used freely with no more database penalty.
As the old Monday Night Football IBM ads use to say, You Make The Call.
Your colleague is right, you are wrong.
It has nothing to do with
recordCountbeing a reference or not. Accesing afielddoes not have side effects.If
recordCountwere apropertythen depending on the getter implementation you could have a database lookup, which is precisely what happens inRecordCount.