I saw in many libraries, when returning some result, is used return a = new A() (e.g. return entrySet = new EntrySet()) instead of returning just new EntrySet(), what’s the difference ?
I saw in many libraries, when returning some result, is used return a =
Share
return a = new A()returns the value of the assignment ofnew A()toa. The value of an assignment is the value that was assigned.So they both return the exact same value.
However
return a = new A()also assigns the value toa. Ifais a local variable, then this is an assignment that won’t have any effect and should be removed. Ifais a field, then this might be used to remember the last returned value for some reason.In the latter case it will work, but I’d say that it’s bad style (that line does more than 1 thing. Actually it does 3 things) and I’d rewrite it like this: