How to convert object reference that are in string form to that refrence class object?

69 views Asked by At

String string = "org.springframework.web.multipart.commons.CommonsMultipartFile@1c08114a";

// CommonsMultipartFile reference = string // Wrong

// Something like that.....

1

There are 1 answers

2
cfrick On

Short answer: you can't. And more important: you shouldn't. Don't call toString() on your object in the first place. And if someone else hands you this down, beat them with a stick.

Long answer: the default toString() you are seeing there is just to make you differentiate objects. the hashcode/address/pointer there would only allow reversing back to a object, if you either store them somewhere or you have the means to calculate it back (e.g. a Boolean could work (but don't even bother trying)). This is not C, where you could attempt to dash that number into an void* and hope the best.

Disclaimer: it might not be impossible to do, but if you have to do it, then there is something fundamental broken. This answer intentionally does not answer the question.

For reference see Why does the default Object.toString() include the hashcode?