oop - Can a Java constructor return already existing object of same type? -


whenever call constructor in java, creates new object , returns reference in end (of newly created object).

is there possibility java constructor not create new object return reference created object?

// possible myobject not new object, existing object myclass myobject = new myclass(); 

i have list of objects of class, , based on few parameters in constructor more efficient don't create new object, instead pick existing object. there other way?

no. constructors definition run when new object created initialize it. if constructor run, new object has come existence, , there's nothing can it.

what make static method either creates new object, or returns existing one. standard approach in such cases.

say, boolean.valueof(boolean value) in standard library exists purpose of avoiding creation of objects. can create them using new boolean(value), better call method because return same object same values.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -