java : Advantages of anonymous object -
i have 1 class named sample used in code.
class sample{ . . object somemethod(){ return someobject; } . . }
i call :
object ob = new sample().somemethod();
i want know there advantage if create anonymous object of class( new sample()) , call require method if don't have further use of object. benefits?
i assume asking code posted contrasted following:
sample s = new sample(); s.somemethod();
(where explicitly assign new sample()
local variable).
there's no significant performance or memory benefit 1 way or another. if store reference in local variable , invoke method, suppose there may (extremely) small performance penalty storing reference. however, suspect many compilers notice variable dead once method called , optimize away assignment. jit compiler might finish job. we're talking few cpu cycles @ most.
Comments
Post a Comment